diff --git a/ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc b/ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc index 0547a2fcf596f..d6e8393d45051 100644 --- a/ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc +++ b/ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc @@ -1179,8 +1179,18 @@ void DesktopWindowTreeHostWin::HandleMove() { // Adding/removing a monitor, or changing the primary monitor can cause a // WM_MOVE message before `OnDisplayChanged()`. Without this call, we would // DCHECK due to stale `DisplayInfo`s. See https:://crbug.com/1413940. + // UpdateDisplayInfosIfNeeded() may synchronously notify DisplayObservers, + // one of which may close this Widget and free `this` (see the matching + // guard in HWNDMessageHandler::OnDpiChanged). + auto ref = GetWeakPtr(); display::win::GetScreenWin()->UpdateDisplayInfosIfNeeded(); + if (!ref) { + return; + } CheckForMonitorChange(); + if (!ref) { + return; + } OnHostMovedInPixels(); } diff --git a/ui/views/win/hwnd_message_handler.cc b/ui/views/win/hwnd_message_handler.cc index 16751cc7016a7..7e7324a565b71 100644 --- a/ui/views/win/hwnd_message_handler.cc +++ b/ui/views/win/hwnd_message_handler.cc @@ -2377,7 +2377,13 @@ LRESULT HWNDMessageHandler::OnInputEvent(UINT message, } void HWNDMessageHandler::OnMove(const gfx::Point& point) { + // HandleMove() may synchronously destroy `this` via DisplayObserver + // notifications (see DesktopWindowTreeHostWin::HandleMove). + auto ref = msg_handler_weak_factory_.GetWeakPtr(); delegate_->HandleMove(); + if (!ref) { + return; + } SetMsgHandled(FALSE); }