From 6a278ea43e7d49d7339206a311a63dc2ee291b01 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 29 Jan 2024 11:20:20 +0100 Subject: QmlModels: Explicitly clear list data dynamic metaobject Otherwise it can try to access model objects after they are deleted. Pick-to: 6.7 Fixes: QTBUG-121588 Change-Id: I4420611be19dc650b7bf29ae805a7477e4c3d7c0 Reviewed-by: Fabian Kosmale Reviewed-by: Nicolas Fella Reviewed-by: Qt CI Bot --- .../qqmlinstantiator/data/listDataDestruction.qml | 20 ++++++++++++++++++++ .../qml/qqmlinstantiator/tst_qqmlinstantiator.cpp | 12 ++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/auto/qml/qqmlinstantiator/data/listDataDestruction.qml (limited to 'tests/auto/qml/qqmlinstantiator') diff --git a/tests/auto/qml/qqmlinstantiator/data/listDataDestruction.qml b/tests/auto/qml/qqmlinstantiator/data/listDataDestruction.qml new file mode 100644 index 0000000000..82d707af37 --- /dev/null +++ b/tests/auto/qml/qqmlinstantiator/data/listDataDestruction.qml @@ -0,0 +1,20 @@ +import QtQml + +QtObject { + id: menu + + Component.onCompleted: dt = new Date(); + property date dt + property Instantiator i: Instantiator { + model: { + var model = []; + var d = menu.dt; + model.push({text: "A"}); + return model; + } + delegate: QtObject { + objectName: modelData.text + Component.onCompleted: menu.objectName = objectName + } + } +} diff --git a/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp b/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp index f6d2752889..6db46d04a8 100644 --- a/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp +++ b/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp @@ -36,6 +36,8 @@ private slots: void handlerWithParent(); void boundDelegateComponent(); + + void listDataDestruction(); }; tst_qqmlinstantiator::tst_qqmlinstantiator() @@ -308,6 +310,16 @@ void tst_qqmlinstantiator::boundDelegateComponent() QCOMPARE(b->objectAt(2)->objectName(), QStringLiteral("root3")); } +void tst_qqmlinstantiator::listDataDestruction() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("listDataDestruction.qml")); + QVERIFY2(component.isReady(), qPrintable(component.errorString())); + QScopedPointer o(component.create()); + QVERIFY(!o.isNull()); + QCOMPARE(o->objectName(), QLatin1String("A")); +} + class SingleBoolItemModel : public QAbstractListModel { Q_OBJECT -- cgit v1.2.3