diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 27cc711f17..c0aa30a227 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -1122,6 +1122,12 @@ void WebContents::InitWithWebContents( } WebContents::~WebContents() { + // DevTools frontend messages use base::Unretained delegate callbacks. + // Clear the delegate before other teardown work can trigger callbacks + // into this partially destroyed WebContents. + if (inspectable_web_contents_) + inspectable_web_contents_->GetView()->SetDelegate(nullptr); + if (web_contents()) { auto* permission_manager = static_cast( web_contents()->GetBrowserContext()->GetPermissionControllerDelegate()); @@ -1129,9 +1135,6 @@ WebContents::~WebContents() { permission_manager->CancelPendingRequests(web_contents()); } - if (inspectable_web_contents_) - inspectable_web_contents_->GetView()->SetDelegate(nullptr); - if (owner_window_) { owner_window_->RemoveBackgroundThrottlingSource(this); }