[PyQt] pyqt circular references & garbage collection

Phil Thompson phil at riverbankcomputing.co.uk
Tue Jul 31 22:44:03 BST 2007


On Tuesday 31 July 2007 9:11 pm, charles chen wrote:
>    Thanks Phil for your help with our memory lapse.
>
>    We've come across another issue:  the Python garbage collector isn't
> sussing out circular references properly.
>
>    A previous post by you say that this should be working:
>
> http://www.riverbankcomputing.com/pipermail/pyqt/2007-May/016237.html
>
>    Here's a simple demonstration:
> >>> from PyQt4.QtCore import QObject
> >>> class q(QObject):
>
> ...   def init(self):
> ...     QObject.init(self)
> ...   def __del__(self):
> ...     print 'q deleted.'
> ...
>
> >>> q1 = q()
> >>> q1 = None
>
> q deleted.
>
> >>> q2 = q()
> >>> q2.circularReference = q2
> >>> q2 = None
> >>> q3 = q()
> >>> q3.circularReference = q3
> >>> q3.deleteLater()
> >>> q3 = None
> >>> import gc
> >>> gc.collect()
>
> 0
>
> >>> q4 = q()
> >>> q4.circularReference = q4
> >>> q4.circularReference = None
> >>> q4 = None
>
> q deleted.
>
>    q2 and q3 are never gc'ed.
>
>    We're also seeing this in production.  Are we doing something wrong?

I don't believe that the garbage collector operates on classes that have a 
__del__ method. See the comments in gcmodule.c in the Python source code.

Phil


More information about the PyQt mailing list