From 5efe6549e5248fbe5b20c8a18fec64d248d7b87c Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Mon, 22 Mar 2010 16:47:18 +1000 Subject: Fix Loader crash. If setting the source of a Loader triggered a change to the source (i.e. the thing being loaded set the source to something else) a crash would occur. We now no longer delete the component immediately when the source changes. Task-number: QTBUG-9241 --- .../graphicsitems/qdeclarativeloader.cpp | 11 +++++++++- .../qdeclarativeloader/data/BlueRect.qml | 8 ++++++++ .../qdeclarativeloader/data/GreenRect.qml | 7 +++++++ .../declarative/qdeclarativeloader/data/crash.qml | 14 +++++++++++++ .../qdeclarativeloader/tst_qdeclarativeloader.cpp | 24 ++++++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/auto/declarative/qdeclarativeloader/data/BlueRect.qml create mode 100644 tests/auto/declarative/qdeclarativeloader/data/GreenRect.qml create mode 100644 tests/auto/declarative/qdeclarativeloader/data/crash.qml diff --git a/src/declarative/graphicsitems/qdeclarativeloader.cpp b/src/declarative/graphicsitems/qdeclarativeloader.cpp index 6dbcd16..745734e 100644 --- a/src/declarative/graphicsitems/qdeclarativeloader.cpp +++ b/src/declarative/graphicsitems/qdeclarativeloader.cpp @@ -66,7 +66,7 @@ void QDeclarativeLoaderPrivate::itemGeometryChanged(QDeclarativeItem *resizeItem void QDeclarativeLoaderPrivate::clear() { if (ownComponent) { - delete component; + component->deleteLater(); component = 0; ownComponent = false; } @@ -285,7 +285,16 @@ void QDeclarativeLoaderPrivate::_q_sourceLoaded() return; } + QDeclarativeComponent *c = component; QObject *obj = component->create(ctxt); + if (component != c) { + // component->create could trigger a change in source that causes + // component to be set to something else. In that case we just + // need to cleanup. + delete obj; + delete ctxt; + return; + } if (obj) { ctxt->setParent(obj); item = qobject_cast(obj); diff --git a/tests/auto/declarative/qdeclarativeloader/data/BlueRect.qml b/tests/auto/declarative/qdeclarativeloader/data/BlueRect.qml new file mode 100644 index 0000000..3b49f6a --- /dev/null +++ b/tests/auto/declarative/qdeclarativeloader/data/BlueRect.qml @@ -0,0 +1,8 @@ +import Qt 4.6 + +Rectangle { + objectName: "blue" + width: 100 + height: 100 + color: "blue" +} diff --git a/tests/auto/declarative/qdeclarativeloader/data/GreenRect.qml b/tests/auto/declarative/qdeclarativeloader/data/GreenRect.qml new file mode 100644 index 0000000..7ee3513 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeloader/data/GreenRect.qml @@ -0,0 +1,7 @@ +import Qt 4.6 + +Rectangle { + width: 100; height: 100 + color: "green" + Component.onCompleted: myLoader.source = "BlueRect.qml" +} diff --git a/tests/auto/declarative/qdeclarativeloader/data/crash.qml b/tests/auto/declarative/qdeclarativeloader/data/crash.qml new file mode 100644 index 0000000..8474e78 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeloader/data/crash.qml @@ -0,0 +1,14 @@ +import Qt 4.6 + +Rectangle { + width: 400 + height: 400 + + function setLoaderSource() { + myLoader.source = "GreenRect.qml" + } + + Loader { + id: myLoader + } +} diff --git a/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp b/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp index 61b2800..2c20836 100644 --- a/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp +++ b/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp @@ -88,6 +88,8 @@ private slots: void failNetworkRequest(); // void networkComponent(); + void deleteComponentCrash(); + private: QDeclarativeEngine engine; }; @@ -459,6 +461,28 @@ void tst_QDeclarativeLoader::failNetworkRequest() delete loader; } +// QTBUG-9241 +void tst_QDeclarativeLoader::deleteComponentCrash() +{ + QDeclarativeComponent component(&engine, TEST_FILE("/crash.qml")); + QDeclarativeItem *item = qobject_cast(component.create()); + QVERIFY(item); + + item->metaObject()->invokeMethod(item, "setLoaderSource"); + + QDeclarativeLoader *loader = qobject_cast(item->QGraphicsObject::children().at(0)); + QVERIFY(loader); + QVERIFY(loader->item()); + QCOMPARE(loader->item()->objectName(), QLatin1String("blue")); + QCOMPARE(loader->progress(), 1.0); + QCOMPARE(loader->status(), QDeclarativeLoader::Ready); + QCOMPARE(static_cast(loader)->children().count(), 1); + QEXPECT_FAIL("", "QTBUG-9245", Continue); + QVERIFY(loader->source() == QUrl::fromLocalFile(SRCDIR "/data/BlueRect.qml")); + + delete item; +} + QTEST_MAIN(tst_QDeclarativeLoader) #include "tst_qdeclarativeloader.moc" -- cgit v0.12