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

Alberto Mardegan mardy at users.sourceforge.net
Sun May 1 21:14:47 CEST 2022


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


More information about the Development mailing list