[pygtk] Garbage collection prematurely clears cyclical objects referenced from GTK callbacks

Mark Seaborn mseaborn at cmedresearch.com
Wed Aug 13 18:28:44 WST 2008


On Wed, 2008-08-13 at 11:29 +0200, Hrvoje Nikšić wrote:

> Traceback (most recent call last):
>   File "bug.py", line 9, in <lambda>
>     w.connect('delete-event', lambda *args: self.die())
> NameError: free variable 'self' referenced before assignment in enclosing scope

This looks like the bug I reported last week:
http://bugzilla.gnome.org/show_bug.cgi?id=546802
- "Pygtk destroys cycle too early"

The assignment "self.window = w" creates a cycle between the Buggy
object, the gtk.Window() and the callback.  The cycle is wrongly freed
by Python's GC because Pygobject does not tell Python that the widget's
wrapper object is referenced by the underlying GObject.  As you have
found, in order to break the cycle Python zeros out the callback
closure's captured free variables, which is what causes the "referenced
before assignment" exception.

Regards,
Mark
-- 
Mark Seaborn
Software Engineer

Cmed Technology Ltd.
Registered in England and Wales No. 3869835
Registered Office and Address for Communication:
Holmwood, Broadlands Business Campus,
Langhurstwood Road, Horsham, RH12 4QP, United Kingdom

E mseaborn at cmedresearch.com
W www.cmedresearch.com



More information about the pygtk mailing list