Always delete object if incubator is cancelled
authorAaron Kennedy <aaron.kennedy@nokia.com>
Wed, 5 Oct 2011 08:15:03 +0000 (18:15 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 6 Oct 2011 05:55:45 +0000 (07:55 +0200)
Change-Id: Iad04340bac9d1345069e540e6bf9dbf671ce9226
Reviewed-on: http://codereview.qt-project.org/6088
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/declarative/qml/qdeclarativeincubator.cpp
tests/auto/declarative/qdeclarativeincubator/data/clearDuringCompletion.qml [new file with mode: 0644]
tests/auto/declarative/qdeclarativeincubator/testtypes.cpp
tests/auto/declarative/qdeclarativeincubator/testtypes.h
tests/auto/declarative/qdeclarativeincubator/tst_qdeclarativeincubator.cpp

index ed75c9a..4779caf 100644 (file)
@@ -486,6 +486,8 @@ void QDeclarativeIncubator::clear()
     if (s == Loading) {
         Q_ASSERT(d->component);
         enginePriv = QDeclarativeEnginePrivate::get(d->component->engine);
+        delete d->result;
+        d->result = 0;
     }
 
     d->clear();
diff --git a/tests/auto/declarative/qdeclarativeincubator/data/clearDuringCompletion.qml b/tests/auto/declarative/qdeclarativeincubator/data/clearDuringCompletion.qml
new file mode 100644 (file)
index 0000000..556f460
--- /dev/null
@@ -0,0 +1,6 @@
+import Qt.test 1.0
+
+SelfRegistering {
+    property variant a: CompletionRegistering {}
+    property variant b: CompletionRegistering {}
+}
index c1f07ac..3ff15a7 100644 (file)
@@ -58,7 +58,32 @@ void SelfRegisteringType::clearMe()
     m_me = 0;
 }
 
+CompletionRegisteringType *CompletionRegisteringType::m_me = 0;
+CompletionRegisteringType::CompletionRegisteringType()
+{
+}
+
+void CompletionRegisteringType::classBegin()
+{
+}
+
+void CompletionRegisteringType::componentComplete()
+{
+    m_me = this;
+}
+
+CompletionRegisteringType *CompletionRegisteringType::me()
+{
+    return m_me;
+}
+
+void CompletionRegisteringType::clearMe()
+{
+    m_me = 0;
+}
+
 void registerTypes()
 {
     qmlRegisterType<SelfRegisteringType>("Qt.test", 1,0, "SelfRegistering");
+    qmlRegisterType<CompletionRegisteringType>("Qt.test", 1,0, "CompletionRegistering");
 }
index a023410..85ee4a6 100644 (file)
@@ -42,6 +42,7 @@
 #define TESTTYPES_H
 
 #include <QtCore/qobject.h>
+#include <QDeclarativeParserStatus>
 
 class SelfRegisteringType : public QObject
 {
@@ -62,6 +63,22 @@ private:
     int m_v;
 };
 
+class CompletionRegisteringType : public QObject, public QDeclarativeParserStatus
+{
+Q_OBJECT
+public:
+    CompletionRegisteringType();
+
+    virtual void classBegin();
+    virtual void componentComplete();
+
+    static CompletionRegisteringType *me();
+    static void clearMe();
+
+private:
+    static CompletionRegisteringType *m_me;
+};
+
 void registerTypes();
 
 #endif // TESTTYPES_H
index e7fc777..f870ffb 100644 (file)
@@ -77,6 +77,7 @@ private slots:
     void noIncubationController();
     void forceCompletion();
     void setInitialState();
+    void clearDuringCompletion();
 
 private:
     QDeclarativeIncubationController controller;
@@ -395,6 +396,36 @@ void tst_qdeclarativeincubator::setInitialState()
     }
 }
 
+void tst_qdeclarativeincubator::clearDuringCompletion()
+{
+    CompletionRegisteringType::clearMe();
+    SelfRegisteringType::clearMe();
+
+    QDeclarativeComponent component(&engine, TEST_FILE("clearDuringCompletion.qml"));
+    QVERIFY(component.isReady());
+
+    QDeclarativeIncubator incubator;
+    component.create(incubator);
+
+    QCOMPARE(incubator.status(), QDeclarativeIncubator::Loading);
+    QVERIFY(CompletionRegisteringType::me() == 0);
+
+    while (CompletionRegisteringType::me() == 0 && incubator.isLoading()) {
+        bool b = false;
+        controller.incubateWhile(&b);
+    }
+
+    QVERIFY(CompletionRegisteringType::me() != 0);
+    QVERIFY(SelfRegisteringType::me() != 0);
+    QVERIFY(incubator.isLoading());
+
+    QPointer<QObject> srt = SelfRegisteringType::me();
+
+    incubator.clear();
+    QVERIFY(incubator.isNull());
+    QVERIFY(srt.isNull());
+}
+
 QTEST_MAIN(tst_qdeclarativeincubator)
 
 #include "tst_qdeclarativeincubator.moc"