From e7eae05a3c45b239b5510cb566947c2334749b56 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 12 Dec 2018 15:56:30 +0100 Subject: Avoid memory leaks in QQmlComponent test Change-Id: I33f5f72bf90ba1ba6aef5668fab660421c722ae6 Reviewed-by: Simon Hausmann --- tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp | 91 ++++++++++++---------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp b/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp index efd5bb571b..7c7c7d3bd0 100644 --- a/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp +++ b/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp @@ -173,8 +173,8 @@ void tst_qqmlcomponent::qmlCreateWindow() QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(testFileUrl("createWindow.qml")); - QQuickWindow* window = qobject_cast(component.create()); - QVERIFY(window); + QScopedPointer window(qobject_cast(component.create())); + QVERIFY(!window.isNull()); } void tst_qqmlcomponent::qmlCreateObjectAutoParent_data() @@ -192,8 +192,8 @@ void tst_qqmlcomponent::qmlCreateObjectAutoParent() QQmlEngine engine; QQmlComponent component(&engine, testFileUrl(testFile)); - QQuickItem *root = qobject_cast(component.create()); - QVERIFY(root); + QScopedPointer root(qobject_cast(component.create())); + QVERIFY(!root.isNull()); QObject *qtobjectParent = root->property("qtobjectParent").value(); QQuickItem *itemParent = qobject_cast(root->property("itemParent").value()); QQuickWindow *windowParent = qobject_cast(root->property("windowParent").value()); @@ -251,45 +251,52 @@ void tst_qqmlcomponent::qmlCreateObjectWithProperties() QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("createObjectWithScript.qml")); QVERIFY2(component.errorString().isEmpty(), component.errorString().toUtf8()); - QObject *object = component.create(); - QVERIFY(object != nullptr); + QScopedPointer object(component.create()); + QVERIFY(!object.isNull()); - QObject *testObject1 = object->property("declarativerectangle").value(); - QVERIFY(testObject1); - QCOMPARE(testObject1->parent(), object); - QCOMPARE(testObject1->property("x").value(), 17); - QCOMPARE(testObject1->property("y").value(), 17); - QCOMPARE(testObject1->property("color").value(), QColor(255,255,255)); - QCOMPARE(QQmlProperty::read(testObject1,"border.width").toInt(), 3); - QCOMPARE(QQmlProperty::read(testObject1,"innerRect.border.width").toInt(), 20); - delete testObject1; - - QObject *testObject2 = object->property("declarativeitem").value(); - QVERIFY(testObject2); - QCOMPARE(testObject2->parent(), object); - //QCOMPARE(testObject2->metaObject()->className(), "QDeclarativeItem_QML_2"); - QCOMPARE(testObject2->property("x").value(), 17); - QCOMPARE(testObject2->property("y").value(), 17); - QCOMPARE(testObject2->property("testBool").value(), true); - QCOMPARE(testObject2->property("testInt").value(), 17); - QCOMPARE(testObject2->property("testObject").value(), object); - delete testObject2; - - QObject *testBindingObj = object->property("bindingTestObject").value(); - QVERIFY(testBindingObj); - QCOMPARE(testBindingObj->parent(), object); - QCOMPARE(testBindingObj->property("testValue").value(), 300); - object->setProperty("width", 150); - QCOMPARE(testBindingObj->property("testValue").value(), 150 * 3); - delete testBindingObj; - - QObject *testBindingThisObj = object->property("bindingThisTestObject").value(); - QVERIFY(testBindingThisObj); - QCOMPARE(testBindingThisObj->parent(), object); - QCOMPARE(testBindingThisObj->property("testValue").value(), 900); - testBindingThisObj->setProperty("width", 200); - QCOMPARE(testBindingThisObj->property("testValue").value(), 200 * 3); - delete testBindingThisObj; + { + QScopedPointer testObject1(object->property("declarativerectangle") + .value()); + QVERIFY(testObject1); + QCOMPARE(testObject1->parent(), object.data()); + QCOMPARE(testObject1->property("x").value(), 17); + QCOMPARE(testObject1->property("y").value(), 17); + QCOMPARE(testObject1->property("color").value(), QColor(255,255,255)); + QCOMPARE(QQmlProperty::read(testObject1.data(),"border.width").toInt(), 3); + QCOMPARE(QQmlProperty::read(testObject1.data(),"innerRect.border.width").toInt(), 20); + } + + { + QScopedPointer testObject2(object->property("declarativeitem").value()); + QVERIFY(testObject2); + QCOMPARE(testObject2->parent(), object.data()); + //QCOMPARE(testObject2->metaObject()->className(), "QDeclarativeItem_QML_2"); + QCOMPARE(testObject2->property("x").value(), 17); + QCOMPARE(testObject2->property("y").value(), 17); + QCOMPARE(testObject2->property("testBool").value(), true); + QCOMPARE(testObject2->property("testInt").value(), 17); + QCOMPARE(testObject2->property("testObject").value(), object.data()); + } + + { + QScopedPointer testBindingObj(object->property("bindingTestObject") + .value()); + QVERIFY(testBindingObj); + QCOMPARE(testBindingObj->parent(), object.data()); + QCOMPARE(testBindingObj->property("testValue").value(), 300); + object->setProperty("width", 150); + QCOMPARE(testBindingObj->property("testValue").value(), 150 * 3); + } + + { + QScopedPointer testBindingThisObj(object->property("bindingThisTestObject") + .value()); + QVERIFY(testBindingThisObj); + QCOMPARE(testBindingThisObj->parent(), object.data()); + QCOMPARE(testBindingThisObj->property("testValue").value(), 900); + testBindingThisObj->setProperty("width", 200); + QCOMPARE(testBindingThisObj->property("testValue").value(), 200 * 3); + } } void tst_qqmlcomponent::qmlCreateParentReference() -- cgit v1.2.3