Tkinter destroy() and mem leaks

mpotter27 at my-deja.com.bbs mpotter27 at my-deja.com.bbs
Wed Jul 12 16:00:10 EDT 2000


Hi,

I have what is probably a rather naive question, but here goes...

I puzzled as to why invoking the destroy() method on a widget fails to
remove the reference to it's master? As far as I can tell from the code
it removes the references in master.children, removes all it's own
children entries, etc.. but retains it's self.master reference.

For example:

>>> from Tkinter import *
>>> import sys
>>> f = Frame()
>>> b = Button(f)
>>> sys.getrefcount(f)
4
>>> f.destroy()
>>> sys.getrefcount(f)
3
>>> b.master
<Tkinter.Frame instance at 10280840>
>>> f.children
{}
>>> b.master = None
>>> sys.getrefcount(f)
2

This typically isn't a problem as long as the master doesn't hold any
references to the child other than via .children. However, the
situation that made me notice this was one in which I made a simple-
minded "mega-widget" by subclassing Frame and having it store
references to a number of subwidgets as attributes:

class myFrame(Frame):

    def __init__(self,parent = None):
        Frame.__init__(self,parent)
        self.button = Button(self,command=self.callback)

    def callback(self):
        pass

If used so:

frame = myFrame()
frame.destroy()
del(frame)

It leaks memory as the button references the myFrame instance
via .master and myFrame references the button via self.button.

Now one could avoid this by structuring the code differently, but it
seems that simply having Tkinter.BaseWidget.destroy set self.master =
None would solve the problem. It seems that destroy should remove all
the Tkinter specific "stuff" from the python proxy object. However I'm
sure there's a reason it doesn't. I'd be grateful if someone could tell
me what it is.

Sorry for the long post!

Mike Potter

Center for Advanced Research in Biotechnology
9600 Gudelsky Dr
Rockville, MD 20850
potterm at erols.com


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list