macOS: Warn the user when KVO-observing QCocoaNSWindow

We recreate the window of our QNSView in various situation such as changing
the styleMask, which results in either a new QNSwindow or QNSpanel being
set as the window of the view. KVO-observing the window property of the
view will fail we then have a new window instance, and will crash when
trying to remove the observer from the new window.

Unfortunately catching the NSRangeException that happens when removing
an observer from a 'switched' window is not possible, so the user is
left with:

  Terminating app due to uncaught exception 'NSRangeException', reason:
    Cannot remove an observer <Foo 0xabc> for the key path "bar"
    from <QNSPanel 0xdef> because it is not registered as an observer.

Change-Id: Ib2adbb99f19303a054fb1eb65e959aecd32b1162
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
bb10
Tor Arne Vestbø 2018-02-16 15:35:32 +01:00
parent df3da2c1d1
commit 5d365b96b9
1 changed files with 5 additions and 0 deletions

View File

@ -1262,6 +1262,11 @@ void QCocoaWindow::recreateWindowIfNeeded()
if ((isContentView() && !shouldBeContentView) || (recreateReason & PanelChanged)) {
if (m_nsWindow) {
qCDebug(lcQpaCocoaWindow) << "Getting rid of existing window" << m_nsWindow;
if (m_nsWindow.observationInfo) {
qCCritical(lcQpaCocoaWindow) << m_nsWindow << "has active key-value observers (KVO)!"
<< "These will stop working now that the window is recreated, and will result in exceptions"
<< "when the observers are removed. Break in QCocoaWindow::recreateWindowIfNeeded to debug.";
}
[m_nsWindow closeAndRelease];
if (isContentView()) {
// We explicitly disassociate m_view from the window's contentView,