Mark objects from Component.createObject() as destructible
authorAaron Kennedy <aaron.kennedy@nokia.com>
Fri, 29 Jul 2011 03:53:35 +0000 (13:53 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 29 Jul 2011 07:39:41 +0000 (09:39 +0200)
Change-Id: I00a1a2b5cca80c3e2ea097690cadf21581e1356d
Task-number: QTBUG-20626
Reviewed-on: http://codereview.qt.nokia.com/2367
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/declarative/qml/qdeclarativecomponent.cpp
tests/auto/declarative/qdeclarativeecmascript/data/dynamicDeletion.2.qml [new file with mode: 0644]
tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp

index e99928d..b6bcc64 100644 (file)
@@ -748,6 +748,10 @@ void QDeclarativeComponent::createObject(QDeclarativeV8Function *args)
 
     completeCreate();
     
+    QDeclarativeData *ddata = QDeclarativeData::get(ret);
+    Q_ASSERT(ddata);
+    ddata->setImplicitDestructible();
+
     RETURN(object);
 
 #undef RETURN
diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/dynamicDeletion.2.qml b/tests/auto/declarative/qdeclarativeecmascript/data/dynamicDeletion.2.qml
new file mode 100644 (file)
index 0000000..70c2816
--- /dev/null
@@ -0,0 +1,21 @@
+import QtQuick 1.0
+
+QtObject {
+    id: root
+
+    property QtObject objectProperty
+
+    property Component c: Component {
+        id: componentObject
+        QtObject {
+        }
+    }
+
+    function create() {
+        objectProperty = c.createObject(root);
+    }
+    
+    function destroy() {
+        objectProperty.destroy();
+    }
+}
index bc33932..b1bc5bd 100644 (file)
@@ -1074,6 +1074,7 @@ void tst_qdeclarativeecmascript::dynamicCreation()
 */
 void tst_qdeclarativeecmascript::dynamicDestruction()
 {
+    {
     QDeclarativeComponent component(&engine, TEST_FILE("dynamicDeletion.qml"));
     QDeclarativeGuard<MyQmlObject> object = qobject_cast<MyQmlObject*>(component.create());
     QVERIFY(object != 0);
@@ -1102,6 +1103,27 @@ void tst_qdeclarativeecmascript::dynamicDestruction()
     QTest::qWait(0);
     QCoreApplication::instance()->processEvents(QEventLoop::DeferredDeletion);
     QVERIFY(!object);
+    }
+
+    {
+    QDeclarativeComponent component(&engine, TEST_FILE("dynamicDeletion.2.qml"));
+    QObject *o = component.create();
+    QVERIFY(o != 0);
+
+    QVERIFY(qvariant_cast<QObject*>(o->property("objectProperty")) == 0);
+
+    QMetaObject::invokeMethod(o, "create");
+
+    QVERIFY(qvariant_cast<QObject*>(o->property("objectProperty")) != 0);
+
+    QMetaObject::invokeMethod(o, "destroy");
+
+    QCoreApplication::instance()->processEvents(QEventLoop::DeferredDeletion);
+
+    QVERIFY(qvariant_cast<QObject*>(o->property("objectProperty")) == 0);
+
+    delete o;
+    }
 }
 
 /*