diff options
-rw-r--r-- | src/qml/qml/qqmlmetatype.cpp | 7 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/data/singletonTest.qml | 1 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/data/singletonTest2.qml | 2 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/testtypes.cpp | 33 |
4 files changed, 42 insertions, 1 deletions
diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp index 26271e3f03..1a27a487bd 100644 --- a/src/qml/qml/qqmlmetatype.cpp +++ b/src/qml/qml/qqmlmetatype.cpp @@ -242,7 +242,12 @@ void QQmlType::SingletonInstanceInfo::destroy(QQmlEngine *e) // cleans up the engine-specific singleton instances if they exist. scriptApis.remove(e); QObject *o = qobjectApis.take(e); - delete o; + if (o) { + QQmlData *ddata = QQmlData::get(o, false); + if (ddata && ddata->indestructible) + return; + delete o; + } } void QQmlType::SingletonInstanceInfo::setQObjectApi(QQmlEngine *e, QObject *o) diff --git a/tests/auto/qml/qqmlecmascript/data/singletonTest.qml b/tests/auto/qml/qqmlecmascript/data/singletonTest.qml index 9e41bd4e2c..ca3784322a 100644 --- a/tests/auto/qml/qqmlecmascript/data/singletonTest.qml +++ b/tests/auto/qml/qqmlecmascript/data/singletonTest.qml @@ -45,4 +45,5 @@ Item { property bool qobjectTest: MyInheritedQmlObjectSingleton.isItYouQObject(MyInheritedQmlObjectSingleton) property bool myQmlObjectTest: MyInheritedQmlObjectSingleton.isItYouMyQmlObject(MyInheritedQmlObjectSingleton) property bool myInheritedQmlObjectTest: MyInheritedQmlObjectSingleton.isItYouMyInheritedQmlObject(MyInheritedQmlObjectSingleton) + property int testFoo: TestTypeCppSingleton.foo } diff --git a/tests/auto/qml/qqmlecmascript/data/singletonTest2.qml b/tests/auto/qml/qqmlecmascript/data/singletonTest2.qml index 67654dd81f..ef08745812 100644 --- a/tests/auto/qml/qqmlecmascript/data/singletonTest2.qml +++ b/tests/auto/qml/qqmlecmascript/data/singletonTest2.qml @@ -52,6 +52,7 @@ Item { property bool qobjectTest2: false property bool qobjectTest3: false property bool singletonEqualToItself: true + property int testFoo: -1 Component.onCompleted: { MyInheritedQmlObjectSingleton.myInheritedQmlObjectProperty = MyInheritedQmlObjectSingleton; @@ -70,5 +71,6 @@ Item { qobjectTest3 = MyInheritedQmlObjectSingleton == MyInheritedQmlObjectSingleton.qobjectProperty; singletonEqualToItself = MyInheritedQmlObjectSingleton == MyInheritedQmlObjectSingleton; + testFoo = TestTypeCppSingleton.foo } } diff --git a/tests/auto/qml/qqmlecmascript/testtypes.cpp b/tests/auto/qml/qqmlecmascript/testtypes.cpp index edfd97b750..285158a4ea 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.cpp +++ b/tests/auto/qml/qqmlecmascript/testtypes.cpp @@ -325,6 +325,38 @@ bool MyInheritedQmlObject::isItYouMyInheritedQmlObject(MyInheritedQmlObject *o) return o && o == theSingletonObject; } +class TestTypeCppSingleton : public QObject +{ + Q_OBJECT + Q_PROPERTY(int foo READ foo) + + Q_CLASSINFO("DefaultProperty", "foo") +public: + int foo() { return 0; } + static TestTypeCppSingleton *instance() { + static TestTypeCppSingleton cppSingleton; + return &cppSingleton; + } +private: + TestTypeCppSingleton(){} + ~TestTypeCppSingleton() { + // just to make sure it crashes on double delete + static int a = 0; + static int *ptr = &a; + *ptr = 1; + ptr = 0; + } +}; + +QObject *testTypeCppProvider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine); + Q_UNUSED(scriptEngine); + TestTypeCppSingleton *o = TestTypeCppSingleton::instance(); + QQmlEngine::setObjectOwnership(o, QQmlEngine::CppOwnership); + return o; +} + static QObject *create_singletonWithEnum(QQmlEngine *, QJSEngine *) { return new SingletonWithEnum; @@ -391,6 +423,7 @@ void registerTypes() qmlRegisterType<MyQmlObject>("Qt.test", 1,0, "MyQmlObjectAlias"); qmlRegisterType<MyQmlObject>("Qt.test", 1,0, "MyQmlObject"); qmlRegisterSingletonType<MyInheritedQmlObject>("Test", 1, 0, "MyInheritedQmlObjectSingleton", inheritedQmlObject_provider); + qmlRegisterSingletonType<TestTypeCppSingleton>("Test", 1, 0, "TestTypeCppSingleton", testTypeCppProvider); qmlRegisterType<MyDeferredObject>("Qt.test", 1,0, "MyDeferredObject"); qmlRegisterType<MyVeryDeferredObject>("Qt.test", 1,0, "MyVeryDeferredObject"); qmlRegisterType<MyQmlContainer>("Qt.test", 1,0, "MyQmlContainer"); |