From e64a66bfd31dce94b2754579678a49f97c349129 Mon Sep 17 00:00:00 2001 From: CezaryKulakowski <50166166+CezaryKulakowski@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:28:36 +0200 Subject: [PATCH] fix: detach native view when its removed from parent on macOS (#43627) Right now DelayedNativeViewHost attaches its underlying native view when it's being attached to a widget but it doesn't detach it when it's being detached. It may lead to use-after-free and crash. --- shell/browser/ui/cocoa/delayed_native_view_host.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/browser/ui/cocoa/delayed_native_view_host.mm b/shell/browser/ui/cocoa/delayed_native_view_host.mm index 6d594e964717..78a5668a4dcc 100644 --- a/shell/browser/ui/cocoa/delayed_native_view_host.mm +++ b/shell/browser/ui/cocoa/delayed_native_view_host.mm @@ -15,6 +15,8 @@ DelayedNativeViewHost::~DelayedNativeViewHost() = default; void DelayedNativeViewHost::ViewHierarchyChanged( const views::ViewHierarchyChangedDetails& details) { + if (!details.is_add && native_view()) + Detach(); NativeViewHost::ViewHierarchyChanged(details); if (details.is_add && GetWidget() && !native_view()) Attach(native_view_);