Hello Gianluca, interesting point indeed.
However double constructors seems like a workaround to me for the chicken and the egg problem :) Then, obviously, if it works it works. No question about that ! :) Massimo ----- Messaggio originale ----- Da: Gianluca <gmax...@gmail.com> A: Massimo Callegari <massimocalleg...@yahoo.it> Cc: "development@qt-project.org" <development@qt-project.org> Inviato: Martedì 28 Aprile 2015 12:21 Oggetto: Re: [Development] QQmlEngine and ObjectOwnership Hello Massimo, I got crazy last year for the same reason … and one developer drive me in the right direction and solved the problem in a slightly different way of you. In my code, sometimes the object is created from QML side … and sometimes is created from C++. In these situations, the ideal behavior should be (of course for me), that object created in C++ are not owned by QML … but as you find, the situation is not like that. So, another strangeness of the owernship by QML … is that depends on the parent of the object created :-S I discovered that if the parent is a QObject created by me outside QML (so, in C++ code) the QML doesn’t take ownership. So, that’s my weird solution: two constructor 1) one set as parent a singleton object created from C++ code, and I use it when I need to create the code from C++ and pass the object to QML 2) another that doesn’t set the parent, and I use it from QML and in this way, then the QML destroy it whenever it wants and it’s ok because this object never pass to C++. Ciao, Gianluca. Il giorno 28/apr/2015, alle ore 12:08, Massimo Callegari <massimocalleg...@yahoo.it> ha scritto: > 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 _______________________________________________ Development mailing list Development@qt-project.org http://lists.qt-project.org/mailman/listinfo/development