aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/qml/qml/qqmlmetatype.cpp16
-rw-r--r--tests/auto/qml/qqmlmetatype/tst_qqmlmetatype.cpp39
2 files changed, 51 insertions, 4 deletions
diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp
index 11806a89a0..708859be00 100644
--- a/src/qml/qml/qqmlmetatype.cpp
+++ b/src/qml/qml/qqmlmetatype.cpp
@@ -497,9 +497,9 @@ QQmlType::QQmlType(QQmlMetaTypeData *data, const QString &elementName, const QQm
d->extraData.cd->attachedPropertiesFunc = type.attachedPropertiesFunction;
d->extraData.cd->attachedPropertiesType = type.attachedPropertiesMetaObject;
if (d->extraData.cd->attachedPropertiesType) {
- QHash<const QMetaObject *, int>::Iterator iter = d->attachedPropertyIds.find(d->baseMetaObject);
- if (iter == d->attachedPropertyIds.end())
- iter = d->attachedPropertyIds.insert(d->baseMetaObject, d->index);
+ auto iter = QQmlTypePrivate::attachedPropertyIds.find(d->baseMetaObject);
+ if (iter == QQmlTypePrivate::attachedPropertyIds.end())
+ iter = QQmlTypePrivate::attachedPropertyIds.insert(d->baseMetaObject, d->index);
d->extraData.cd->attachedPropertiesId = *iter;
} else {
d->extraData.cd->attachedPropertiesId = -1;
@@ -569,8 +569,16 @@ QQmlType::QQmlType(QQmlTypePrivate *priv)
QQmlType::~QQmlType()
{
- if (d && !d->refCount.deref())
+ if (d && !d->refCount.deref()) {
+ // If attached properties were successfully registered, deregister them.
+ // (They may not have been registered if some other type used the same baseMetaObject)
+ if (d->regType == CppType && d->extraData.cd->attachedPropertiesType) {
+ auto it = QQmlTypePrivate::attachedPropertyIds.find(d->baseMetaObject);
+ if (it != QQmlTypePrivate::attachedPropertyIds.end() && *it == d->index)
+ QQmlTypePrivate::attachedPropertyIds.erase(it);
+ }
delete d;
+ }
}
QHashedString QQmlType::module() const
diff --git a/tests/auto/qml/qqmlmetatype/tst_qqmlmetatype.cpp b/tests/auto/qml/qqmlmetatype/tst_qqmlmetatype.cpp
index e83dac48fb..7139a1c952 100644
--- a/tests/auto/qml/qqmlmetatype/tst_qqmlmetatype.cpp
+++ b/tests/auto/qml/qqmlmetatype/tst_qqmlmetatype.cpp
@@ -35,6 +35,7 @@
#include <private/qqmlmetatype_p.h>
#include <private/qqmlpropertyvalueinterceptor_p.h>
+#include <private/qqmlengine_p.h>
#include <private/qhashedstring_p.h>
#include "../../shared/util.h"
@@ -64,6 +65,7 @@ private slots:
void unregisterCustomSingletonType();
void normalizeUrls();
+ void unregisterAttachedProperties();
};
class TestType : public QObject
@@ -533,6 +535,43 @@ void tst_qqmlmetatype::normalizeUrls()
QVERIFY(!QQmlMetaType::qmlType(url, /*includeNonFileImports=*/true).isValid());
}
+void tst_qqmlmetatype::unregisterAttachedProperties()
+{
+ qmlClearTypeRegistrations();
+
+ const QUrl dummy("qrc:///doesnotexist.qml");
+ {
+ QQmlEngine e;
+ QQmlComponent c(&e);
+ c.setData("import QtQuick 2.2\n Item { }", dummy);
+
+ const QQmlType attachedType = QQmlMetaType::qmlType("QtQuick/KeyNavigation", 2, 2);
+ QCOMPARE(attachedType.attachedPropertiesId(QQmlEnginePrivate::get(&e)),
+ attachedType.index());
+
+ QVERIFY(c.create());
+ }
+
+ qmlClearTypeRegistrations();
+ {
+ QQmlEngine e;
+ QQmlComponent c(&e);
+
+ // The extra import shuffles the type IDs around, so that we
+ // get a different ID for the attached properties. If the attached
+ // properties aren't properly cleared, this will crash.
+ c.setData("import QtQml.StateMachine 1.0 \n"
+ "import QtQuick 2.2 \n"
+ "Item { KeyNavigation.up: null }", dummy);
+
+ const QQmlType attachedType = QQmlMetaType::qmlType("QtQuick/KeyNavigation", 2, 2);
+ QCOMPARE(attachedType.attachedPropertiesId(QQmlEnginePrivate::get(&e)),
+ attachedType.index());
+
+ QVERIFY(c.create());
+ }
+}
+
QTEST_MAIN(tst_qqmlmetatype)
#include "tst_qqmlmetatype.moc"