diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2013-09-05 11:00:25 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-09-05 13:01:39 +0200 |
commit | d1a8623ecb4ef9f9942873503af7eb21a420ec7e (patch) | |
tree | e48894bd4d2c966830613fdd0b93f35334351a7b /tests/auto/corelib | |
parent | cc594d2064799f09f8d60981f75354745d98c717 (diff) |
QVariant: Don't crash when doing user-defined conversion to QObject*
The destructor of the ConverterFunctor calls unregisterConverter. If
the instance is static (as it is in qmetatype.h), then this method
can be called after the QGlobalStatic has already been destroyed.
Change-Id: I33b70734cbe09dfa888e887280c349e43ad1b855
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index dab40db0ec..39b16200df 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -154,6 +154,7 @@ private slots: void qvariant_cast_QObject_data(); void qvariant_cast_QObject(); void qvariant_cast_QObject_derived(); + void qvariant_cast_QObject_wrapper(); void toLocale(); @@ -2235,6 +2236,41 @@ void tst_QVariant::qvariant_cast_QObject_derived() } } +struct QObjectWrapper +{ + explicit QObjectWrapper(QObject *o = 0) : obj(o) {} + + QObject* getObject() const { + return obj; + } +private: + QObject *obj; +}; + +Q_DECLARE_METATYPE(QObjectWrapper) + +struct Converter +{ + Converter() {} + + QObject* operator()(const QObjectWrapper &f) const + { + return f.getObject(); + } +}; + +void tst_QVariant::qvariant_cast_QObject_wrapper() +{ + QMetaType::registerConverter<QObjectWrapper, QObject*>(&QObjectWrapper::getObject); + + CustomQObjectDerived *object = new CustomQObjectDerived(this); + QObjectWrapper wrapper(object); + QVariant v = QVariant::fromValue(wrapper); + v.convert(qMetaTypeId<QObject*>()); + QCOMPARE(v.value<QObject*>(), object); + +} + void tst_QVariant::convertToQUint8() const { /* qint8. */ |