[pygtk] Advice on custom form

Jeffrey Barish jeff_barish at earthlink.net
Wed Mar 12 03:44:19 WST 2008


I created a custom widget that draws a form mainly comprising Entry and
Label widgets.  It works fine for generating a static display, but I am
having problems when I need to redraw the form for a different set of data. 
Each set in general requires a different number and arrangement of Entry
and Label widgets, so my first attempt simply removed all children from the
form and then instantiated new widgets.  That solution also appears to work
fine.  However, I believe that I have introduced a memory leak.  The
widgets that I remove from an obsolete display are not garbage collected. 
(I believe that this is true because a __del__ method in one of my custom
subclasses never gets called.)  I have been exploring two solutions.  In
one, I cache the widgets and reuse them whenever they are available.  In
principle, this solution should work, but it is complicating my code.  The
other solution is to delete references to widgets so that they will be
garbage collected.  The issue seems to be that there are cycles.  For
example, one of my collection widgets holds a reference to a child and the
child holds a reference to the collection as its GTK parent.  I want them
both deleted, but because they reference each other, the garbage collector
stays clear.  I have made a halfhearted attempt to find all the references,
but I am skeptical that I will be able to find them all because so many are
made by GTK invisibly.  I like this solution better because I ought to be
able to package the code for unshackling a widget within the widget, which
makes for a cleaner structure.  Before I knock myself out implementing
either solution, I am wondering whether anyone has any advice on how to
handle this problem. I am by no means an expert on the garbage collector,
so it's very possible that I do not even understand the problem correctly.
-- 
Jeffrey Barish



More information about the pygtk mailing list