[Development] Problem with QML garbage collection (Qt 5.15)

Furkan Üzümcü furkanuzumcu at gmail.com
Sun May 1 19:01:24 CEST 2022

I think adding an object to a `ListModel` does not alter its ownership. If the page with the initial property which holds a reference to the object returned from the C++ method is destroyed, then I would expect that object to be destroyed as well. Personally, I usually prefer to control the life time of an object by the C++ side or by instantiated types in the QML side. Unless I’m wrong about `ListModel` not taking ownership in `append`, I’d recommend not tempering with the object ownership rules manually. Also, is there a code sample that you can provide for this?

> On Apr 30, 2022, at 17:40, Alberto Mardegan <mardy at users.sourceforge.net> wrote:
> Hi there!
>  I think I'm experiencing an issue related to
> https://bugreports.qt.io/browse/QTBUG-50319
> In my case, I have a C++ method returning a new QObject, which has automatic Javascript ownership. This is fine, because indeed I want to have my object managed by the QML engine.
> Then I'm passing the object as a property to a subpage, and also storing it into a ListModel (by calling append()). If I navigate to the subpage where I passed my object as a property, I can see that it is working fine. If I then navigate to another page where I have a ListView operating on that ListModel where I added my object, that works fine as well.
> The problem happens when I navigate back from the page with the ListView. It seems to me, that when that page gets destroyed my object gets destroyed as well, despite being still used in the ListModel (which is never getting destroyed).
> I'm working around this by setting the ownership to C++ and never actually destroying the objects (since I have few of them anyways), but I suspect that this might be a bug in the garbage collection.
> Ciao,
>  Alberto
> -- 
> http://www.mardy.it - Geek in un lingua international
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> https://lists.qt-project.org/listinfo/development

More information about the Development mailing list