diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.mm b/src/plugins/platforms/cocoa/qcocoaglcontext.mm index 40a7a6ef5e..026a99a152 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.mm +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.mm @@ -422,6 +422,15 @@ bool QCocoaGLContext::setDrawable(QPlatformSurface *surface) m_updateObservers.append(QMacNotificationObserver([NSApplication sharedApplication], NSApplicationDidChangeScreenParametersNotification, updateCallback)); + m_updateObservers.append(QMacNotificationObserver(view, + QCocoaWindowWillReleaseQNSViewNotification, [this, view] { + if (QT_IGNORE_DEPRECATIONS(m_context.view) != view) + return; + qCDebug(lcQpaOpenGLContext) << view << "about to be released." + << "Clearing current drawable for" << m_context; + [m_context clearDrawable]; + })); + // If any of the observers fire at this point it's fine. We check the // view association (atomically) in the update callback, and skip the // update if we haven't associated yet. Setting the drawable below will diff --git a/src/plugins/platforms/cocoa/qcocoawindow.h b/src/plugins/platforms/cocoa/qcocoawindow.h index e3152f153c..c0684504aa 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.h +++ b/src/plugins/platforms/cocoa/qcocoawindow.h @@ -295,6 +295,8 @@ public: // for QNSView #endif }; +extern const NSNotificationName QCocoaWindowWillReleaseQNSViewNotification; + #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug debug, const QCocoaWindow *window); #endif diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 1bc53bde5c..70cfa42974 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -186,6 +186,8 @@ void QCocoaWindow::initialize() m_initialized = true; } +const NSNotificationName QCocoaWindowWillReleaseQNSViewNotification = @"QCocoaWindowWillReleaseQNSViewNotification"; + QCocoaWindow::~QCocoaWindow() { qCDebug(lcQpaWindow) << "QCocoaWindow::~QCocoaWindow" << window(); @@ -209,6 +211,13 @@ QCocoaWindow::~QCocoaWindow() } #endif + // Must send notification before calling release, as doing it from + // [QNSView dealloc] would mean that any weak references to the view + // would already return nil. + [NSNotificationCenter.defaultCenter + postNotificationName:QCocoaWindowWillReleaseQNSViewNotification + object:m_view]; + [m_view release]; [m_nsWindow close]; [m_nsWindow release];