diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2019-09-30 18:09:22 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2019-10-01 11:18:19 +0200 |
commit | 702249777f9052c0f26793187f36fa0301dee105 (patch) | |
tree | 6d53f6ffa4f1f67939dbab1bfa5cbd23c58e9aa8 | |
parent | 85bf1c438b36d91a77e3371d8fe2d8c3750c4858 (diff) |
tst_qqmlecmascript: Don't leak created objects
The result from component.create() has to be deleted somewhere.
Change-Id: I23135cb639fc316641e399decc740d9f5d445a84
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r-- | tests/auto/qml/qqmlecmascript/testtypes.h | 8 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 101 |
2 files changed, 54 insertions, 55 deletions
diff --git a/tests/auto/qml/qqmlecmascript/testtypes.h b/tests/auto/qml/qqmlecmascript/testtypes.h index 730dc7cab8..3233e7f105 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.h +++ b/tests/auto/qml/qqmlecmascript/testtypes.h @@ -1098,13 +1098,11 @@ class MyItemUsingRevisionedObject : public QObject Q_PROPERTY(MyRevisionedClass *revisioned READ revisioned) public: - MyItemUsingRevisionedObject() { - m_revisioned = new MyRevisionedClass; - } + MyItemUsingRevisionedObject() : m_revisioned (new MyRevisionedClass) {} - MyRevisionedClass *revisioned() const { return m_revisioned; } + MyRevisionedClass *revisioned() const { return m_revisioned.get(); } private: - MyRevisionedClass *m_revisioned; + QScopedPointer<MyRevisionedClass> m_revisioned; }; QML_DECLARE_TYPE(MyRevisionedBaseClassRegistered) diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index fa78e60bba..05ad5e2f22 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -1693,10 +1693,10 @@ void tst_qqmlecmascript::componentCreation() } QQmlComponent component(&engine, testUrl); - MyTypeObject *object = qobject_cast<MyTypeObject*>(component.create()); + QScopedPointer<MyTypeObject> object(qobject_cast<MyTypeObject*>(component.create())); QVERIFY(object != nullptr); - QMetaObject::invokeMethod(object, method.toUtf8()); + QMetaObject::invokeMethod(object.get(), method.toUtf8()); QQmlComponent *created = object->componentProperty(); if (creationError.isEmpty()) { @@ -1704,7 +1704,7 @@ void tst_qqmlecmascript::componentCreation() QObject *expectedParent = reinterpret_cast<QObject *>(quintptr(-1)); if (createdParent == QLatin1String("obj")) { - expectedParent = object; + expectedParent = object.get(); } else if ((createdParent == QLatin1String("null")) || createdParent.isEmpty()) { expectedParent = nullptr; } @@ -3124,13 +3124,13 @@ void tst_qqmlecmascript::callQtInvokables() void tst_qqmlecmascript::resolveClashingProperties() { - ClashingNames *o = new ClashingNames(); + QScopedPointer<ClashingNames> o(new ClashingNames()); QQmlEngine qmlengine; QV4::ExecutionEngine *engine = qmlengine.handle(); QV4::Scope scope(engine); - QV4::ScopedValue object(scope, QV4::QObjectWrapper::wrap(engine, o)); + QV4::ScopedValue object(scope, QV4::QObjectWrapper::wrap(engine, o.get())); QV4::ObjectIterator it(scope, object->as<QV4::Object>(), QV4::ObjectIterator::EnumerableOnly); QV4::ScopedValue name(scope); QV4::ScopedValue value(scope); @@ -6093,7 +6093,7 @@ void tst_qqmlecmascript::variants() QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("variants.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); QCOMPARE(object->property("undefinedVariant").type(), QVariant::Invalid); @@ -6102,13 +6102,13 @@ void tst_qqmlecmascript::variants() QCOMPARE(object->property("doubleVariant").type(), QVariant::Double); QVariant result; - QMetaObject::invokeMethod(object, "checkNull", Q_RETURN_ARG(QVariant, result)); + QMetaObject::invokeMethod(object.get(), "checkNull", Q_RETURN_ARG(QVariant, result)); QCOMPARE(result.toBool(), true); - QMetaObject::invokeMethod(object, "checkUndefined", Q_RETURN_ARG(QVariant, result)); + QMetaObject::invokeMethod(object.get(), "checkUndefined", Q_RETURN_ARG(QVariant, result)); QCOMPARE(result.toBool(), true); - QMetaObject::invokeMethod(object, "checkNumber", Q_RETURN_ARG(QVariant, result)); + QMetaObject::invokeMethod(object.get(), "checkNumber", Q_RETURN_ARG(QVariant, result)); QCOMPARE(result.toBool(), true); } @@ -6998,12 +6998,12 @@ void tst_qqmlecmascript::realToInt() { QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("realToInt.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); - QMetaObject::invokeMethod(object, "test1"); + QMetaObject::invokeMethod(object.get(), "test1"); QCOMPARE(object->value(), int(4)); - QMetaObject::invokeMethod(object, "test2"); + QMetaObject::invokeMethod(object.get(), "test2"); QCOMPARE(object->value(), int(7)); } @@ -7012,7 +7012,7 @@ void tst_qqmlecmascript::urlProperty() QQmlEngine engine; { QQmlComponent component(&engine, testFileUrl("urlProperty.1.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); object->setStringProperty("http://qt-project.org"); QCOMPARE(object->urlProperty(), QUrl("http://qt-project.org/index.html")); @@ -7027,7 +7027,7 @@ void tst_qqmlecmascript::urlPropertyWithEncoding() QQmlEngine engine; { QQmlComponent component(&engine, testFileUrl("urlProperty.2.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); object->setStringProperty("http://qt-project.org"); const QUrl encoded = QUrl::fromEncoded("http://qt-project.org/?get%3cDATA%3e", QUrl::TolerantMode); @@ -7062,7 +7062,7 @@ void tst_qqmlecmascript::dynamicString() { QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("dynamicString.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); QCOMPARE(object->property("stringProperty").toString(), QString::fromLatin1("string:Hello World false:0 true:1 uint32:100 int32:-100 double:3.14159 date:2011-02-11 05::30:50!")); @@ -7072,7 +7072,7 @@ void tst_qqmlecmascript::deleteLaterObjectMethodCall() { QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("deleteLaterObjectMethodCall.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); } @@ -7080,7 +7080,7 @@ void tst_qqmlecmascript::automaticSemicolon() { QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("automaticSemicolon.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); } @@ -7088,7 +7088,7 @@ void tst_qqmlecmascript::compatibilitySemicolon() { QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("compatibilitySemicolon.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); } @@ -7096,7 +7096,7 @@ void tst_qqmlecmascript::incrDecrSemicolon1() { QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("incrDecrSemicolon1.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); } @@ -7104,7 +7104,7 @@ void tst_qqmlecmascript::incrDecrSemicolon2() { QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("incrDecrSemicolon2.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); } @@ -7120,7 +7120,7 @@ void tst_qqmlecmascript::unaryExpression() { QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("unaryExpression.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); } @@ -7260,7 +7260,7 @@ void tst_qqmlecmascript::switchStatement() QQmlEngine engine; { QQmlComponent component(&engine, testFileUrl("switchStatement.1.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); // `object->value()' is the number of executed statements @@ -7283,7 +7283,7 @@ void tst_qqmlecmascript::switchStatement() { QQmlComponent component(&engine, testFileUrl("switchStatement.2.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); // `object->value()' is the number of executed statements @@ -7306,7 +7306,7 @@ void tst_qqmlecmascript::switchStatement() { QQmlComponent component(&engine, testFileUrl("switchStatement.3.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); // `object->value()' is the number of executed statements @@ -7333,7 +7333,7 @@ void tst_qqmlecmascript::switchStatement() QString warning = component.url().toString() + ":4:5: Unable to assign [undefined] to int"; QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); // `object->value()' is the number of executed statements @@ -7357,7 +7357,7 @@ void tst_qqmlecmascript::switchStatement() { QQmlComponent component(&engine, testFileUrl("switchStatement.5.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); // `object->value()' is the number of executed statements @@ -7380,7 +7380,7 @@ void tst_qqmlecmascript::switchStatement() { QQmlComponent component(&engine, testFileUrl("switchStatement.6.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); // `object->value()' is the number of executed statements @@ -7408,7 +7408,7 @@ void tst_qqmlecmascript::withStatement() { QUrl url = testFileUrl("withStatement.1.qml"); QQmlComponent component(&engine, url); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); QCOMPARE(object->value(), 123); @@ -7420,7 +7420,7 @@ void tst_qqmlecmascript::tryStatement() QQmlEngine engine; { QQmlComponent component(&engine, testFileUrl("tryStatement.1.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); QCOMPARE(object->value(), 123); @@ -7428,7 +7428,7 @@ void tst_qqmlecmascript::tryStatement() { QQmlComponent component(&engine, testFileUrl("tryStatement.2.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); QCOMPARE(object->value(), 321); @@ -7436,7 +7436,7 @@ void tst_qqmlecmascript::tryStatement() { QQmlComponent component(&engine, testFileUrl("tryStatement.3.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); QVERIFY(object->qjsvalue().isUndefined()); @@ -7444,7 +7444,7 @@ void tst_qqmlecmascript::tryStatement() { QQmlComponent component(&engine, testFileUrl("tryStatement.4.qml")); - MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QScopedPointer<MyQmlObject> object(qobject_cast<MyQmlObject*>(component.create())); QVERIFY(object != nullptr); QVERIFY(object->qjsvalue().isUndefined()); @@ -7585,7 +7585,7 @@ void tst_qqmlecmascript::onDestruction() // component instance. This shouldn't crash. QQmlEngine engine; QQmlComponent c(&engine, testFileUrl("onDestruction.qml")); - QObject *obj = c.create(); + QScopedPointer<QObject> obj(c.create()); QVERIFY(obj != nullptr); QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); } @@ -7926,19 +7926,19 @@ void tst_qqmlecmascript::dateParse() QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("date.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); if (object == nullptr) qDebug() << component.errorString(); QVERIFY(object != nullptr); QVariant q; - QMetaObject::invokeMethod(object, "test_is_invalid_jsDateTime", Q_RETURN_ARG(QVariant, q)); + QMetaObject::invokeMethod(object.get(), "test_is_invalid_jsDateTime", Q_RETURN_ARG(QVariant, q)); QVERIFY(q.toBool()); - QMetaObject::invokeMethod(object, "test_is_invalid_qtDateTime", Q_RETURN_ARG(QVariant, q)); + QMetaObject::invokeMethod(object.get(), "test_is_invalid_qtDateTime", Q_RETURN_ARG(QVariant, q)); QVERIFY(q.toBool()); - QMetaObject::invokeMethod(object, "test_rfc2822_date", Q_RETURN_ARG(QVariant, q)); + QMetaObject::invokeMethod(object.get(), "test_rfc2822_date", Q_RETURN_ARG(QVariant, q)); QCOMPARE(q.toLongLong(), 1379512851000LL); } @@ -7947,14 +7947,14 @@ void tst_qqmlecmascript::utcDate() QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("utcdate.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); if (object == nullptr) qDebug() << component.errorString(); QVERIFY(object != nullptr); QVariant q; QVariant val = QString::fromLatin1("2014-07-16T23:30:31"); - QMetaObject::invokeMethod(object, "check_utc", Q_RETURN_ARG(QVariant, q), Q_ARG(QVariant, val)); + QMetaObject::invokeMethod(object.get(), "check_utc", Q_RETURN_ARG(QVariant, q), Q_ARG(QVariant, val)); QVERIFY(q.toBool()); } @@ -7963,20 +7963,20 @@ void tst_qqmlecmascript::negativeYear() QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("negativeyear.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); if (object == nullptr) qDebug() << component.errorString(); QVERIFY(object != nullptr); QVariant q; - QMetaObject::invokeMethod(object, "check_negative_tostring", Q_RETURN_ARG(QVariant, q)); + QMetaObject::invokeMethod(object.get(), "check_negative_tostring", Q_RETURN_ARG(QVariant, q)); // Only check for the year. We hope that every language writes the year in arabic numerals and // in relation to a specific dude's date of birth. We also hope that no language adds a "-2001" // junk string somewhere in the middle. QVERIFY(q.toString().indexOf(QStringLiteral("-2001")) != -1); - QMetaObject::invokeMethod(object, "check_negative_toisostring", Q_RETURN_ARG(QVariant, q)); + QMetaObject::invokeMethod(object.get(), "check_negative_toisostring", Q_RETURN_ARG(QVariant, q)); QCOMPARE(q.toString().left(16), QStringLiteral("result: -002000-")); } @@ -8019,6 +8019,7 @@ void tst_qqmlecmascript::jsOwnedObjectsDeletedOnEngineDestroy() QCOMPARE(spy1.count(), 1); QCOMPARE(spy2.count(), 1); + deleteObject.deleteNestedObject(); delete object; } @@ -8030,7 +8031,7 @@ void tst_qqmlecmascript::updateCall() QString file("updateCall.qml"); QQmlEngine engine; QQmlComponent component(&engine, testFileUrl(file)); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); } @@ -8041,7 +8042,7 @@ void tst_qqmlecmascript::numberParsing() QString file("numberParsing.%1.qml"); file = file.arg(i); QQmlComponent component(&engine, testFileUrl(file)); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); } for (int i = 1; i < 3; ++i) { @@ -8224,8 +8225,8 @@ void tst_qqmlecmascript::idsAsLValues() QString err = QString(QLatin1String("%1:5: Error: left-hand side of assignment operator is not an lvalue")).arg(testFileUrl("idAsLValue.qml").toString()); QQmlComponent component(&engine, testFileUrl("idAsLValue.qml")); QTest::ignoreMessage(QtWarningMsg, qPrintable(err)); - MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create()); - QVERIFY(!object); + QScopedPointer<QObject> object(component.create()); + QVERIFY(!qobject_cast<MyQmlObject*>(object.get())); } void tst_qqmlecmascript::qtbug_34792() @@ -8440,10 +8441,10 @@ void tst_qqmlecmascript::readUnregisteredQObjectProperty() qmlRegisterType<ObjectContainer>("Test", 1, 0, "ObjectContainer"); QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("accessUnregisteredQObjectProperty.qml")); - QObject *root = component.create(); + QScopedPointer<QObject> root(component.create()); QVERIFY(root); - QMetaObject::invokeMethod(root, "readProperty"); + QMetaObject::invokeMethod(root.get(), "readProperty"); QCOMPARE(root->property("container").value<ObjectContainer*>()->mGetterCalled, true); } @@ -8452,10 +8453,10 @@ void tst_qqmlecmascript::writeUnregisteredQObjectProperty() qmlRegisterType<ObjectContainer>("Test", 1, 0, "ObjectContainer"); QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("accessUnregisteredQObjectProperty.qml")); - QObject *root = component.create(); + QScopedPointer<QObject> root(component.create()); QVERIFY(root); - QMetaObject::invokeMethod(root, "writeProperty"); + QMetaObject::invokeMethod(root.get(), "writeProperty"); QCOMPARE(root->property("container").value<ObjectContainer*>()->mSetterCalled, true); } |