[PyQt] Lambdas, Closures and Garbage Collection

charles matthew chen charles.chen at temboo.com
Wed Sep 3 14:27:19 BST 2008


Hi Phil,

   Thanks for getting back to us.

On Tue, Sep 2, 2008 at 11:09 PM, Phil Thompson 
<phil at riverbankcomputing.com> wrote:
 > You want to force programmers to keep an explicit reference to a lambda?
 > That rather misses the point doesn't it?

  Yes, however, this behavior is a serious source of memory leaks and 
worse for our project. 

   Let's say the lambda calls a bound method of an instance - that 
instance and everything it has a strong reference to will never be 
garbage collected.  We could explicitly disconnect the lambda/closure 
from the signal, but that too would require us to maintain an explicit 
reference to whatever callable we're using as a slot. 

   That is, my understanding is this: if you connect a lambda A as a 
slot on a signal of QObject B, and lambda A references instance C, C 
will not be garbage collected until QObject B is deleted/gc'd. 

   Actually, I'm not 100% sure yet, but I'm working on isolating a bug 
wherein even in the case that B is deleted and gc'd, lambda A (and hence 
instance C) are not garbage collected.  If I can confirm, I'll post a 
short snippet that reproduces this issue to the list.

On Tue, Sep 2, 2008 at 11:09 PM, Phil Thompson 
<phil at riverbankcomputing.com> wrote:
 > Probably.

   I've looked at the source of SIP and not sure how to go about making 
this patch.  sipConnectRx() and sipDisconnectRx() are a bit complex.  
Can you give us any guidance on the best way to go about this?

Charles


On Tue, Sep 2, 2008 at 11:09 PM, Phil Thompson 
<phil at riverbankcomputing.com> wrote:
 > On Tue, 02 Sep 2008 20:30:20 +0200, charles matthew chen
 > <charles.chen at temboo.com> wrote:
 >>
 >> 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?
 >
 > Probably.
 >
 >> We'd like closures, lambdas, etc. to be garbage collected if no other
 >> strong reference remains to them.
 >
 > You want to force programmers to keep an explicit reference to a lambda?
 > That rather misses the point doesn't it?
 >
 > Phil
 >
 > _______________________________________________
 > PyQt mailing list    PyQt at riverbankcomputing.com
 > http://www.riverbankcomputing.com/mailman/listinfo/pyqt
 >



More information about the PyQt mailing list