tst_qmetaobjectbuilder: Clean-up deallocation of dynamic metaobjects

If a testcase failed before we reached free, the QMetaObject would have
leaked.
Moreover, defer freeing until all tests have run: dynamic QMetaObjects
might create dynamic metatypes. If those were shared, we would run into
issues.

Change-Id: I2edfcb2605e58eaba454bfe0446a6b187a5210df
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Fabian Kosmale 2020-11-18 16:32:46 +01:00
parent 71200ae11d
commit 2cea05fce9

View File

@ -66,10 +66,13 @@ private slots:
void propertyMetaType();
void cleanupTestCase();
private:
static bool checkForSideEffects
(const QMetaObjectBuilder& builder,
QMetaObjectBuilder::AddMembers members);
QList<QMetaObject *> dynamicMetaObjectsPendingFree;
};
struct MetaObjectComparison {
@ -757,13 +760,12 @@ void tst_QMetaObjectBuilder::variantProperty()
QMetaObjectBuilder builder;
builder.addProperty("variant", "const QVariant &");
QMetaObject *meta = builder.toMetaObject();
dynamicMetaObjectsPendingFree.push_back(meta);
QMetaProperty prop = meta->property(meta->propertyOffset());
QCOMPARE(QMetaType::Type(prop.userType()), QMetaType::QVariant);
QCOMPARE(QMetaType::Type(prop.userType()), QMetaType::QVariant);
QCOMPARE(QByteArray(prop.typeName()), QByteArray("QVariant"));
free(meta);
}
void tst_QMetaObjectBuilder::notifySignal()
@ -1007,21 +1009,21 @@ void tst_QMetaObjectBuilder::copyMetaObject()
{
QMetaObjectBuilder builder(&QObject::staticMetaObject);
QMetaObject *meta = builder.toMetaObject();
dynamicMetaObjectsPendingFree.push_back(meta);
auto compared = sameMetaObject(meta, &QObject::staticMetaObject);
QVERIFY2(compared, qPrintable(compared.details));
free(meta);
QMetaObjectBuilder builder2(&staticMetaObject);
meta = builder2.toMetaObject();
dynamicMetaObjectsPendingFree.push_back(meta);
compared = sameMetaObject(meta, &staticMetaObject);
QVERIFY2(compared, qPrintable(compared.details));
free(meta);
QMetaObjectBuilder builder3(&SomethingOfEverything::staticMetaObject);
meta = builder3.toMetaObject();
dynamicMetaObjectsPendingFree.push_back(meta);
compared = sameMetaObject(meta, &SomethingOfEverything::staticMetaObject);
QVERIFY2(compared, qPrintable(compared.details));
free(meta);
}
// Serialize and deserialize a meta object and check that
@ -1032,6 +1034,7 @@ void tst_QMetaObjectBuilder::serialize()
{
QMetaObjectBuilder builder(&SomethingOfEverything::staticMetaObject);
QMetaObject *meta = builder.toMetaObject();
dynamicMetaObjectsPendingFree.push_back(meta);
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly | QIODevice::Append);
@ -1044,11 +1047,10 @@ void tst_QMetaObjectBuilder::serialize()
builder2.deserialize(stream2, references);
builder2.setStaticMetacallFunction(builder.staticMetacallFunction());
QMetaObject *meta2 = builder2.toMetaObject();
dynamicMetaObjectsPendingFree.push_back(meta2);
auto compared = sameMetaObject(meta, meta2);
QVERIFY2(compared, qPrintable(compared.details));
free(meta);
free(meta2);
}
// Partial QMetaObjectBuilder
@ -1102,13 +1104,12 @@ void tst_QMetaObjectBuilder::relocatableData()
QMetaObject *meta = builder.toMetaObject();
auto compared = sameMetaObject(meta, &meta2);
dynamicMetaObjectsPendingFree.push_back(meta);
QVERIFY2(compared, qPrintable(compared.details));
QVERIFY(!meta2.d.extradata);
QVERIFY(!meta2.d.relatedMetaObjects);
QVERIFY(!meta2.d.static_metacall);
free(meta);
}
@ -1745,6 +1746,12 @@ void tst_QMetaObjectBuilder::propertyMetaType()
free(mo);
}
void tst_QMetaObjectBuilder::cleanupTestCase()
{
for (QMetaObject *obj: dynamicMetaObjectsPendingFree)
free(obj);
}
QTEST_MAIN(tst_QMetaObjectBuilder)
#include "tst_qmetaobjectbuilder.moc"