Fix crash-on-exit in qmlscene.
authorMorten Sorvig <morten.sorvig@nokia.com>
Mon, 15 Aug 2011 11:50:22 +0000 (13:50 +0200)
committerRichard Moe Gustavsen <richard.gustavsen@nokia.com>
Thu, 18 Aug 2011 07:50:06 +0000 (09:50 +0200)
Take control over NSWindow deletion to make sure
it's lifetime matches that of the QCocoaWindow.

Change-Id: Ia10006d814345356e6aebe7fa1f9a0e012535786
Reviewed-on: http://codereview.qt.nokia.com/2960
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@nokia.com>
src/plugins/platforms/cocoa/qcocoawindow.h
src/plugins/platforms/cocoa/qcocoawindow.mm
src/plugins/platforms/cocoa/qnswindowdelegate.mm

index 92fc026..035a09b 100644 (file)
@@ -76,6 +76,7 @@ public:
 
     void windowDidMove();
     void windowDidResize();
+    void windowWillClose();
 
     void setCurrentContext(QCocoaGLContext *context);
     QCocoaGLContext *currentContext() const;
index 7e88cdb..80a195d 100644 (file)
@@ -82,6 +82,12 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw)
     [m_nsWindow setDelegate:delegate];
     [m_nsWindow setAcceptsMouseMovedEvents:YES];
 
+    // Prevent Cocoa from releasing the window on close. Qt
+    // handles the close event asynchronously and we want to
+    // make sure that m_nsWindow stays valid until the
+    // QCocoaWindow is deleted by Qt.
+    [m_nsWindow setReleasedWhenClosed : NO];
+
     m_contentView = [[QNSView alloc] initWithQWindow:tlw];
 
     if (tlw->surfaceType() == QWindow::OpenGLSurface) {
@@ -99,7 +105,7 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw)
 
 QCocoaWindow::~QCocoaWindow()
 {
-
+    [m_nsWindow release];
 }
 
 void QCocoaWindow::setGeometry(const QRect &rect)
@@ -178,6 +184,12 @@ void QCocoaWindow::windowDidResize()
         m_glContext->update();
 }
 
+
+void QCocoaWindow::windowWillClose()
+{
+    QWindowSystemInterface::handleCloseEvent(window());
+}
+
 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
 {
     m_glContext = context;
index 6521db5..869ef78 100644 (file)
@@ -75,7 +75,9 @@
 - (void)windowWillClose:(NSNotification *)notification
 {
     Q_UNUSED(notification);
-    QWindowSystemInterface::handleCloseEvent(m_cocoaWindow->window());
+    if (m_cocoaWindow) {
+        m_cocoaWindow->windowWillClose();
+    }
 }
 
 @end