connect()'s refcounting behavior seems buggy, and should be documented

Phil Thompson phil at riverbankcomputing.com
Sat Mar 19 10:04:49 GMT 2022


On 19/03/2022 02:28, Kovid Goyal wrote:
> On Fri, Mar 18, 2022 at 05:26:13PM +0000, Phil Thompson wrote:
>> I've changed the PyQt6 behaviour to keep a reference to bound methods. 
>> I'm
>> not going to change PyQt5 as stability (avoiding unexpected 
>> consequences) is
>> more important than correctness so late in its lifecycle.
> 
> Is the reference a weak or a strong reference? If its a strong 
> reference
> it will create reference cycles. Something as simple as:
> 
> class Widget(QDialog):
> 
>     def __init__(self):
>         QDialog.__init__(self)
>         self.bb = QDialogButtonBox(self)
>         self.bb.accepted.connect(self.accept)
> 
>     def accept(self):
>         ...
> 
> is now a reference cycle that the python garbage collector cannot
> collect.

The garbage collector should be able to detect and break such cycles. 
Can you show a complete script based on the fragment above that 
demonstrates a problem (I can't so far).

The current code was originally written for Python v1.5 - before the 
garbage collector was added to Python.

Phil


More information about the PyQt mailing list