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

Yuya Nishihara yuya at tcha.org
Sun Mar 20 06:40:44 GMT 2022


On Sat, 19 Mar 2022 23:13:14 -0700, Ben Rudiak-Gould wrote:
> When a bound method is passed to connect(), PySide6 doesn't hold a
> reference to the bound method at all: It extracts its two fields, the
> "self" object and the function, and holds a weak reference to "self"
> (some instance of Widget in your example) and a strong reference to
> the function (Widget.accept).

Yep, PySide's behavior makes sense.

> On Sat, Mar 19, 2022 at 3:44 AM Yuya Nishihara <yuya at tcha.org> wrote:
> > class Widget(QDialog):
> >     def __init__(self, parent=None):
> >         [...]
> >         self.bb.accepted.connect(lambda: self.accept()) # strong ref
> >         self.t = QTimer()
> >         self.t.timeout.connect(self.onTimeout)
> >         self.t.start(100)
> >     [...]
> >
> > def kick():
> >     w = Widget()
> >     w.exec()
> >     print('w should ideally be deleted here')
> >
> > [...]  
> 
> If I understand your example correctly, and PT made the change I think
> he did, it won't affect this example.

My point is if the new PyQt6 behavior were incref-ing the bound method
(i.e. connect(self.accept) did effectively connect(lambda: self.accept()),
this example would demonstrate the new unintended/buggy behavior.


More information about the PyQt mailing list