deleteLater() releases Python wrapper immediately (allowing it to be garbage collected), rather than during actual destruction

Phil Thompson phil at riverbankcomputing.com
Sun Sep 6 17:40:43 BST 2026


On 06/09/2026 14:38, Lukas Lang wrote:
> I have run into an unexpected lifetime issue regarding the Python
> wrappers for QObjects: It seems that after calling .deleteLater, the
> Python wrapper object is no longer kept alive by pyQt, potentially
> causing it to be GC'ed before the underlying QObject is actually
> destroyed. This has several consequences, such as:
> 
>   *   Overrides/customizations (such as reimplementations of virtual
> functions) are lost, while the underlying QObject is still alive
>   *   Any Python objects only reachable through the wrapper may also
> be GC'ed before destruction. If those objects are receivers for
> signals of the QObject, the associated slots are not called anymore
> after the Python objects are GC'ed
> 
> 
> Seeing as this has several unexpected consequences, it seems like a bug 
> to me?
> Versions: Python 3.14.7, PyQt 6.11.0, Qt 6.11.0, sip 6.16.0

I'm inclined to agree, although I'm surprised it hasn't come up before.

I was concerned that a fix might break things in other places but it 
seems that the only place that the /TransferThis/ function annotation is 
used in the whole of PyQt is with deleteLater(). The fix will be in the 
next SIP snapshot but, as it's a code generator change you will have to 
rebuild PyQt.

However there is a consequence...  If the C++ instance was created by 
C++ (rather than from calling the Python type) then the Python object 
will never get garbage collected. I could modify the fix so that the 
Python object was kept alive only if could eventually be garbage 
collected - but that may be even more confusing.

I'm also happy to revert the fix (and accept the current behaviour as a 
feature) if people want to persuade me that it's a bad idea.

Phil


More information about the PyQt mailing list