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

Fabian Kosmale fabian.kosmale at qt.io
Mon May 2 08:52:25 CEST 2022

Hi Alberto,

I think your issue might be QTBUG-91390 (ListModel does not keep objects with JS ownership alive in 5.15). That was fixed in Qt 6.2 (https://codereview.qt-project.org/c/qt/qtdeclarative/+/354140), but due to the behaviour change never backported to any version of 5.15. The behaviour in 5.15 is certainly not ideal,  but we didn't want to introduce such a rather drastic change that late in the life-time of 5.15. Using the work-around of manually setting C++ ownership (or ensuring that some other object on the JS heap keeps referencing the object) are afaik the only workarounds that can be used there.

For anyone interested in manually backporting the fix that went into 6.2 to their own Qt build, note that you also need https://codereview.qt-project.org/c/qt/qtdeclarative/+/368105 which fixed a regression caused by the first patch.

Kind regards,

Von: Development <development-bounces at qt-project.org> im Auftrag von Alberto Mardegan <mardy at users.sourceforge.net>
Gesendet: Samstag, 30. April 2022 23:40
An: development at qt-project.org
Betreff: [Development] Problem with QML garbage collection (Qt 5.15)

Hi there!

   I think I'm experiencing an issue related to

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

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.


http://www.mardy.it - Geek in un lingua international
Development mailing list
Development at qt-project.org

More information about the Development mailing list