diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2018-02-12 16:53:55 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2018-02-13 12:19:06 +0000 |
commit | fc333db8f9ea17f8cb1f080f3a6755ea363b087a (patch) | |
tree | b5250b2caad11a5afdeda63191cf22c6eff8cead /tests | |
parent | efc7f85520f19dfc5628704cfb5bbe3bc546297f (diff) |
Fix memory leak with ListModel.get
This is a regression introduced with commit
3cc589c98390992e3ee8a7970dc2913ea857d623, which in turn fixed a leak
with QV4::QObjectWrapper objects. Unfortunately the allocate() call into
the persistent (weak) value storage in the list model introduced a leak
of the weak value itself. This is fixed by replacing the free standing
weak value allocation with the use of the existing jsWrapper weak value
in the declarative data (QQmlData). That weak value is freed property in
the destroy() method of the QV4::QObjectWRapper. The extra QQmlData
allocation is hidden behind a unified allocation, similar to what we do
in void QQmlType::create(QObject **, void **, size_t) const.
Task-number: QTBUG-66189
Change-Id: I5351e3e484542709a6b210e84aa19b14d28e11ad
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
(cherry picked from commit 22d43f74e264626d0c28654c42c91839f9de45b5)
Reviewed-by: Erik Verbruggen <erik.verbruggen@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp b/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp index f5c0e5ddf7..653e7543c0 100644 --- a/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp +++ b/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp @@ -124,6 +124,7 @@ private slots: void modify_through_delegate(); void bindingsOnGetResult(); void stringifyModelEntry(); + void qobjectTrackerForDynamicModelObjects(); }; bool tst_qqmllistmodel::compareVariantList(const QVariantList &testList, QVariant object) @@ -1505,6 +1506,33 @@ void tst_qqmllistmodel::stringifyModelEntry() QCOMPARE(v.toString(), expectedString); } +void tst_qqmllistmodel::qobjectTrackerForDynamicModelObjects() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData( + "import QtQuick 2.0\n" + "Item {\n" + " ListModel {\n" + " id: testModel\n" + " objectName: \"testModel\"\n" + " ListElement { name: \"Joe\"; age: 22 }\n" + " }\n" + "}\n", QUrl()); + QScopedPointer<QObject> scene(component.create()); + QQmlListModel *model = scene->findChild<QQmlListModel*>("testModel"); + QQmlExpression expr(engine.rootContext(), model, "get(0);"); + QVariant v = expr.evaluate(); + QVERIFY2(!expr.hasError(), QTest::toString(expr.error().toString())); + + QObject *obj = v.value<QObject*>(); + QVERIFY(obj); + + QQmlData *ddata = QQmlData::get(obj, /*create*/false); + QVERIFY(ddata); + QVERIFY(!ddata->jsWrapper.isNullOrUndefined()); +} + QTEST_MAIN(tst_qqmllistmodel) #include "tst_qqmllistmodel.moc" |