qmlscene: ensure that the window & component get destructed
authorJ-P Nurmi <jpnurmi@digia.com>
Thu, 4 Jul 2013 10:21:04 +0000 (12:21 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 4 Jul 2013 12:35:03 +0000 (14:35 +0200)
Task-number: QTBUG-32207
Change-Id: Ic7c45228cbdc049d3dfdb3482296d0e8c89a930c
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>

tools/qmlscene/main.cpp

index 8a48445..244e0af 100644 (file)
@@ -44,6 +44,8 @@
 #include <QtCore/qdir.h>
 #include <QtCore/qmath.h>
 #include <QtCore/qdatetime.h>
+#include <QtCore/qpointer.h>
+#include <QtCore/qscopedpointer.h>
 
 #include <QtGui/QGuiApplication>
 
@@ -461,7 +463,7 @@ int main(int argc, char ** argv)
             // TODO: as soon as the engine construction completes, the debug service is
             // listening for connections.  But actually we aren't ready to debug anything.
             QQmlEngine engine;
-            QQmlComponent *component = new QQmlComponent(&engine);
+            QPointer<QQmlComponent> component = new QQmlComponent(&engine);
             for (int i = 0; i < imports.size(); ++i)
                 engine.addImportPath(imports.at(i));
             for (int i = 0; i < bundles.size(); ++i)
@@ -481,15 +483,14 @@ int main(int argc, char ** argv)
             }
 
             QObject *topLevel = component->create();
-            QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
-            QQuickView* qxView = 0;
+            QScopedPointer<QQuickWindow> window(qobject_cast<QQuickWindow *>(topLevel));
             if (window) {
                 engine.setIncubationController(window->incubationController());
             } else {
                 QQuickItem *contentItem = qobject_cast<QQuickItem *>(topLevel);
                 if (contentItem) {
-                    qxView = new QQuickView(&engine, NULL);
-                    window = qxView;
+                    QQuickView* qxView = new QQuickView(&engine, NULL);
+                    window.reset(qxView);
                     // Set window default properties; the qml can still override them
                     QString oname = contentItem->objectName();
                     window->setTitle(oname.isEmpty() ? QString::fromLatin1("qmlscene") : QString::fromLatin1("qmlscene: ") + oname);
@@ -534,14 +535,10 @@ int main(int argc, char ** argv)
 #ifdef QML_RUNTIME_TESTING
             RenderStatistics::printTotalStats();
 #endif
-            // Ready to exit.  If we created qxView, it owns the component;
-            // otherwise, the ownership is still right here.  Nobody deletes the engine
-            // (which is odd since the container constructor takes the engine pointer),
-            // but it's stack-allocated anyway.
-            if (qxView)
-                delete qxView;
-            else
-                delete component;
+            // Ready to exit. Notice that the component might be owned by
+            // QQuickView if one was created. That case is tracked by
+            // QPointer, so it is safe to delete the component here.
+            delete component;
         }
     }