From 6fc1e0fc36f0732e79375a0e105816eb17893962 Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Wed, 14 Oct 2015 14:18:58 +0300 Subject: Don't delete singletons objects owned by C++. This way we can enable the usage of a single singletons in both worlds. Currently singletons are destructed before the other QML types, and using a singleton that is owned by C++ we don't need to care about the destruction order anymore. Change-Id: I120fcb8659e16321ae6e70c7b0d62be3bc650d73 Reviewed-by: Simon Hausmann --- .../auto/qml/qqmlecmascript/data/singletonTest.qml | 1 + .../qml/qqmlecmascript/data/singletonTest2.qml | 2 ++ tests/auto/qml/qqmlecmascript/testtypes.cpp | 33 ++++++++++++++++++++++ 3 files changed, 36 insertions(+) (limited to 'tests/auto') 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("Qt.test", 1,0, "MyQmlObjectAlias"); qmlRegisterType("Qt.test", 1,0, "MyQmlObject"); qmlRegisterSingletonType("Test", 1, 0, "MyInheritedQmlObjectSingleton", inheritedQmlObject_provider); + qmlRegisterSingletonType("Test", 1, 0, "TestTypeCppSingleton", testTypeCppProvider); qmlRegisterType("Qt.test", 1,0, "MyDeferredObject"); qmlRegisterType("Qt.test", 1,0, "MyVeryDeferredObject"); qmlRegisterType("Qt.test", 1,0, "MyQmlContainer"); -- cgit v1.2.3