diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2019-10-14 14:06:06 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2019-10-16 08:19:56 +0200 |
commit | 03196c9a0f1635ce78cf53addb6b688108f66fae (patch) | |
tree | 8f7d484419d80cfcb1a70bf98763ca43041dc41f | |
parent | 383854f482a34cef14f918c2a3172522249ef542 (diff) |
Check for invalid context in QQmlContextPrivate::dropDestroyedQObject
If the context is invalid we won't be able to look up the object by name
anymore. In that case there is nothing to do.
Fixes: QTBUG-78326
Change-Id: I011ccb6b02a84725c1d5eae24b494516ae2d5fee
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r-- | src/qml/qml/qqmlcontext.cpp | 3 | ||||
-rw-r--r-- | tests/auto/qml/qqmlcontext/tst_qqmlcontext.cpp | 25 |
2 files changed, 21 insertions, 7 deletions
diff --git a/src/qml/qml/qqmlcontext.cpp b/src/qml/qml/qqmlcontext.cpp index af8deb085c..f75a076bcb 100644 --- a/src/qml/qml/qqmlcontext.cpp +++ b/src/qml/qml/qqmlcontext.cpp @@ -532,6 +532,9 @@ QObject *QQmlContextPrivate::context_at(QQmlListProperty<QObject> *prop, int ind void QQmlContextPrivate::dropDestroyedQObject(const QString &name, QObject *destroyed) { + if (!data->isValid()) + return; + const int idx = data->propertyNames().value(name); Q_ASSERT(idx >= 0); if (qvariant_cast<QObject *>(propertyValues[idx]) != destroyed) diff --git a/tests/auto/qml/qqmlcontext/tst_qqmlcontext.cpp b/tests/auto/qml/qqmlcontext/tst_qqmlcontext.cpp index d9cb6673df..6754f22049 100644 --- a/tests/auto/qml/qqmlcontext/tst_qqmlcontext.cpp +++ b/tests/auto/qml/qqmlcontext/tst_qqmlcontext.cpp @@ -895,16 +895,27 @@ void tst_qqmlcontext::contextObjectHierarchy() void tst_qqmlcontext::destroyContextProperty() { - QQmlEngine engine; - QQmlContext context(&engine); - + QScopedPointer<QQmlContext> context; + QScopedPointer<QObject> objectThatOutlivesEngine(new QObject); { - QObject object; - context.setContextProperty(QLatin1String("a"), &object); - QCOMPARE(qvariant_cast<QObject *>(context.contextProperty(QLatin1String("a"))), &object); + QQmlEngine engine; + context.reset(new QQmlContext(&engine)); + + { + QObject object; + context->setContextProperty(QLatin1String("a"), &object); + QCOMPARE(qvariant_cast<QObject *>(context->contextProperty(QLatin1String("a"))), &object); + } + + QCOMPARE(qvariant_cast<QObject *>(context->contextProperty(QLatin1String("a"))), nullptr); + context->setContextProperty(QLatin1String("b"), objectThatOutlivesEngine.data()); } - QCOMPARE(qvariant_cast<QObject *>(context.contextProperty(QLatin1String("a"))), nullptr); + // dropDestroyedObject() should not crash, even if the engine is gone. + objectThatOutlivesEngine.reset(); + + // We're not allowed to call context->contextProperty("b") anymore. + // TODO: Or are we? } QTEST_MAIN(tst_qqmlcontext) |