From 52767b8ee754f97abe42e9cdc3bcf69365a401fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 27 Mar 2017 17:34:48 +0200 Subject: [PATCH] macOS: Disassociate NSView from contentView when switching NSWindows When a NSView is the contentView of a NSWindow, it still has a superview, in this case an NSThemeFrame, and the theme frame is the view that retains the view, not the contentView property of the window. Unfortunately, when a view is removed from the NSThemeFrame by means of [NSView removeFromSuperview], or indirectly via [NSView addSubview:], AppKit does not update the contentView property of the corresponding NSWindow. The result is that the NSView might be deallocated while the NSWindow still is around, with a stale contentView property. To work around this we explicitly disassociate the view from the NSWindow when we know that we are going to release the NSWindow. This will take care of both resetting the property to nil, and remove the view from the NSThemeFrame superview. Task-number: QTBUG-59734 Change-Id: I1111d492f52fe5bdf86bbae071421f6a8a7a38b8 Reviewed-by: Timur Pocheptsov Reviewed-by: Jake Petroules --- src/plugins/platforms/cocoa/qcocoawindow.mm | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index cfe829905c..f6606745f1 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -1613,6 +1613,13 @@ void QCocoaWindow::recreateWindowIfNeeded() [m_nsWindow closeAndRelease]; if (isChildNSWindow()) [m_view.window.parentWindow removeChildWindow:m_view.window]; + if (isContentView()) { + // We explicitly disassociate m_view from the window's contentView, + // as AppKit does not automatically do this in response to removing + // the view from the NSThemeFrame subview list, so we might end up + // with a NSWindow contentView pointing to a deallocated NSView. + m_view.window.contentView = nil; + } m_nsWindow = 0; }