[PyQt] Lambdas, Closures and Garbage Collection

charles matthew chen charles.chen at temboo.com
Tue Sep 2 19:30:20 BST 2008

Hi Phil,

   The documented behavior of sip/pyqt - to keep strong references to 
slots except instance methods - isn't desirable from our project's point 
of view.  Is there any easy way to patch SIP to disable this behavior?  
We'd like closures, lambdas, etc. to be garbage collected if no other 
strong reference remains to them.


from http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#pyqt-slots-and-qt-slots

Note that when a slot is a Python callable its reference count is not 
increased. This means that a class instance can be deleted without 
having to explicitly disconnect any signals connected to its methods. 
However, if a slot is a lambda function or a partial function then its 
reference count is automatically incremented to prevent it from being 
immediately garbage collected.

from http://www.riverbankcomputing.co.uk/static/Downloads/sip4/ChangeLog

2007/07/14 15:31:37  phil
Rather than only lambda functions being given an extra reference when used as a
slot, anything other that a method or wrapped C function is given an extra
reference.  Specifically this means that partial functions can now be used as

2007/02/18 16:00:12  phil

Fixed a bug in the clearing of reference cycles with lambda slots.  Although
the slot is visited, it is no longer cleared - that is left to the proxy dtor.

2007/01/21 16:32:45  phil
Extended the cyclic garbage collector support for lambda slots so it works with
SIGNALs as well as PYSIGNALs.
Incremented the SIP API version number to 3.4.

2007/01/21 14:55:47  phil
Fixed garbage collection support for lambda slots (at the moment only when the
slot is connected to a PYSIGNAL).

More information about the PyQt mailing list