From 25e26270a1ec0ed838f009d8694f3507af1b0554 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Wed, 3 Mar 2021 16:12:07 +0100 Subject: QQmlIncubator: handle clear inside setinitialState Fixes: QTBUG-91519 Change-Id: Idfe3116c2e94b8e96300d72e15db0bc78425f517 Reviewed-by: Ulf Hermann (cherry picked from commit 2cb306c194625626957fcde44bd56473b0436f83) Reviewed-by: Qt Cherry-pick Bot --- src/qml/qml/qqmlincubator.cpp | 2 +- tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp | 33 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/qml/qml/qqmlincubator.cpp b/src/qml/qml/qqmlincubator.cpp index 6a0210c153..7d4ffcec3f 100644 --- a/src/qml/qml/qqmlincubator.cpp +++ b/src/qml/qml/qqmlincubator.cpp @@ -331,7 +331,7 @@ void QQmlIncubatorPrivate::incubate(QQmlInstantiationInterrupt &i) ddata->rootObjectInCreation = false; if (q) { q->setInitialState(result); - if (!creator->requiredProperties().empty()) { + if (creator && !creator->requiredProperties().empty()) { const auto& unsetRequiredProperties = creator->requiredProperties(); for (const auto& unsetRequiredProperty: unsetRequiredProperties) errors << QQmlComponentPrivate::unsetRequiredPropertyToQQmlError(unsetRequiredProperty); diff --git a/tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp b/tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp index 1650a3a1d6..ac26e8d9f3 100644 --- a/tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp +++ b/tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp @@ -71,6 +71,7 @@ private slots: void contextDelete(); void garbageCollection(); void requiredProperties(); + void deleteInSetInitialState(); private: QQmlIncubationController controller; @@ -1232,6 +1233,38 @@ void tst_qqmlincubator::requiredProperties() } } +class DeletingIncubator : public QQmlIncubator +{ + + + // QQmlIncubator interface +protected: + void statusChanged(Status) override + { + + } + void setInitialState(QObject *obj) override + { + delete obj; + clear(); + } +}; + +void tst_qqmlincubator::deleteInSetInitialState() +{ + QQmlComponent component(&engine, testFileUrl("requiredProperty.qml")); + QVERIFY(component.isReady()); + // forceCompletion immediately after creating an asynchronous object completes it + DeletingIncubator incubator; + incubator.setInitialProperties({{"requiredProperty", 42}}); + QVERIFY(incubator.isNull()); + component.create(incubator); + QVERIFY(incubator.isLoading()); + incubator.forceCompletion(); // no crash + QVERIFY(incubator.isNull()); + QCOMPARE(incubator.object(), nullptr); // object was deleted +} + QTEST_MAIN(tst_qqmlincubator) #include "tst_qqmlincubator.moc" -- cgit v1.2.3