Re: [Development] Problem with QML garbage collection (Qt 5.15)
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 > 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@qt-project.org > https://lists.qt-project.org/listinfo/development ___ Development mailing list Development@qt-project.org https://lists.qt-project.org/listinfo/development
Re: [Development] Problem with QML garbage collection (Qt 5.15)
Hi Furkan, On 01/05/22 20:01, Furkan Üzümcü wrote: 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. mmm... I don't know. My understanding of the documentation is different: "When the object is returned to QML as the return value of a method call, QML will track it and delete it if there are no remaining JavaScript references to it and it has no QObject::parent()." My object is a QObject with no parent, so I would expect the QML engine to track the object (even across property assignments and insertion into models) with some reference counting, and only destroy it when *all* references are dropped. 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. Well, if I don't tamper with the ownership, then it will get Javascript ownership and get destroyed too soon, when I'm still using it in the model. Also, is there a code sample that you can provide for this? I'm afraid I don't have time for that, but if you are interested, the code is all in this branch: https://gitlab.com/mardy/mitubo/-/tree/download Steps to reproduce: 1. Revert this commit: YoutubeDl: set C++ ownership on downloads (f5cb1d1c) 2. qbs run 3. Search for something (e.g., "ciao") 4. In the results, pick any item and press "More", then "Download", the the Download button again 5. Without waiting for the download to complete, press "back" (<) until you return to the main page 6. Click on the menu (the hamburger icon), press Downloads Now if you repeat steps 3-6 once more, on step 6 the download data will be missing and you'll see warnings in the console. Ciao, Alberto -- http://www.mardy.it - Geek in un lingua international ___ Development mailing list Development@qt-project.org https://lists.qt-project.org/listinfo/development
Re: [Development] Problem with QML garbage collection (Qt 5.15)
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, Fabian Von: Development im Auftrag von Alberto Mardegan Gesendet: Samstag, 30. April 2022 23:40 An: development@qt-project.org Betreff: [Development] Problem with QML garbage collection (Qt 5.15) 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@qt-project.org https://lists.qt-project.org/listinfo/development ___ Development mailing list Development@qt-project.org https://lists.qt-project.org/listinfo/development
Re: [Development] Problem with QML garbage collection (Qt 5.15)
Hi Fabian, On 02/05/22 09:52, Fabian Kosmale wrote: 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. [...] I see, thanks for the pointers! In my current case setting the ownership to C++ is acceptable (in any case, the model would be alive for the whole lifetime of the application, so it does not make a practical difference), but it good to know that there's a solution for those who strongly need it. Ciao, Alberto -- http://www.mardy.it - Geek in un lingua international ___ Development mailing list Development@qt-project.org https://lists.qt-project.org/listinfo/development