[PyQt] deleteLater vs setParent(None)

Phil Thompson phil at riverbankcomputing.com
Wed May 11 09:38:34 BST 2016


On 11 May 2016, at 8:24 am, Elvis Stansvik <elvstone at gmail.com> wrote:
> 
> 2016-05-10 18:42 GMT+02:00 Dave Gradwell <davegradwell at yahoo.co.uk>:
>> Hi Elvis,
>> 
>> I’m doing something similar which works for me in production if that helps at all.
>> I preferred the explicitness of deleteLater, although I think either technique would work?
>> 
>> Best, Dave.
> 
> Thanks a lot for sharing your code Dave, looks very similar indeed. I
> also like the explicitness of deleteLater better.
> 
> The reason I asked is precisely this wording by Phil:
> 
> "Changing the ownership also removes that extra reference so that the
> object is available to be garbage collected when it goes out of
> scope."
> 
> I'm not sure if that means deleteLater will not be effective because
> of this extra reference that PyQt keeps or not? But thinking about it
> a bit more, it probably will be effective, since I guess if the object
> is deleted on the C++ side (deleteLater), and PyQt discovers that the
> only reference is its own, then it will let go of that reference to
> let Python object be garbage collected?
> 
> Phil, would you care to comment on this? I realize I don't have a full
> understanding of how this all works yet.
> 
> My goal is to not accumulate memory in this slot, so I'd like both the
> underlying QProcess object to be cleaned up, as well as any Python
> instances of my subclass Process.

With setParent() you are manipulating the Python side of things - getting the garbage collector to do the work.

With deleteLater() you are manipulating the C++ side of things - PyQt reacts when the C++ instance is destroyed.

Bugs aside both should achieve the same thing - but at different times. Personally I always use deleteLater().

Phil


More information about the PyQt mailing list