Hi everyone,

I want to share my experience with the garbage collection of registered classes 
through qmlRegisterType.

I got crazy to understand why my application was randomly crashing and I hope 
this will help others in the future.

Basically if you want to expose a C++ class in the QML world, you do something 
like:

qmlRegisterType<MyClass>("com.myapp.classes", 1, 0, "MyClass");

Then you can use the class methods exposed with Q_PROPERTY in QML like this: 
MyClass.myProperty

The thing is that when dealing with class pointers, the QML engine performs 
garbage collection of the exposed classes when it no longer needs them.

If your classes are created in C++ (new MyClass()) at some point in the C++ 
code, you will find that the class has been destroyed by QML !! This causes a 
bunch of segFaults like there is no tomorrow.

So, after getting crazy to discover this, I discovered also that there is a 
method of QQmlEngine to
assign the ownership of a class:

QQmlEngine::setObjectOwnership(myNewClass, QQmlEngine::CppOwnership);


After adding this simple line after a "myNewClass = new MyClass()", everything 
started to working properly.

Note that in my application, classes created in C++ exist as long as the 
application lives.
Now, I found pretty counter-intuitive that a class created in C++ is actually 
owned by QML (
QQmlEngine::JavaScriptOwnership)


I believe the default ownership should be CppOwnership if the class is created 
in C++ and then, in case, the developer can do the other way around, letting 
QML to handle the destruction of QObjects.

Cheers,

Massimo
_______________________________________________
Development mailing list
Development@qt-project.org
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to