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 <timur.pocheptsov@qt.io>
Reviewed-by: Jake Petroules <jake.petroules@qt.io>
This commit is contained in:
Tor Arne Vestbø 2017-03-27 17:34:48 +02:00
parent 0ec871dac8
commit 52767b8ee7

View File

@ -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;
}