Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Unified Diff: ui/views/controls/menu/menu_controller.cc

Issue 2641983003: Reland Fix MenuController Heap-use-after-free (Closed)
Patch Set: Apply Fix Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | ui/views/controls/menu/menu_controller_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/menu/menu_controller.cc
diff --git a/ui/views/controls/menu/menu_controller.cc b/ui/views/controls/menu/menu_controller.cc
index 95dd4f9a9de9b687b422ecd8e10584fe9590fc8b..79ff77c75f2c375f07f12cac10116b21a5829cb0 100644
--- a/ui/views/controls/menu/menu_controller.cc
+++ b/ui/views/controls/menu/menu_controller.cc
@@ -2575,9 +2575,13 @@ void MenuController::ExitAsyncRun() {
bool nested = delegate_stack_.size() > 1;
// ExitMenuRun unwinds nested delegates
internal::MenuControllerDelegate* delegate = delegate_;
+ // MenuController may have been deleted when releasing ViewsDelegate ref.
+ // However as |delegate| can outlive this, it must still be notified of the
+ // menu closing so that it can perform teardown.
+ int accept_event_flags = accept_event_flags_;
MenuItemView* result = ExitMenuRun();
delegate->OnMenuClosed(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
- result, accept_event_flags_);
+ result, accept_event_flags);
// MenuController may have been deleted by |delegate|.
if (GetActiveInstance() && nested && exit_type_ == EXIT_ALL)
ExitAsyncRun();
@@ -2589,6 +2593,10 @@ MenuItemView* MenuController::ExitMenuRun() {
if (async_run_ && ViewsDelegate::GetInstance())
ViewsDelegate::GetInstance()->ReleaseRef();
+ // Releasing the lock can result in Chrome shutting down, deleting this.
+ if (!GetActiveInstance())
+ return nullptr;
+
// Close any open menus.
SetSelection(nullptr, SELECTION_UPDATE_IMMEDIATELY | SELECTION_EXIT);
« no previous file with comments | « no previous file | ui/views/controls/menu/menu_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698