Python/Tkinter/tk crash [long]

Eric Brunel eric.brunel at pragmadev.com
Thu Aug 14 12:47:59 EDT 2003


Martin Franklin wrote:
> On Thursday 14 August 2003 10:10 am, Eric Brunel wrote:
>>There's a simple workaround, but with drawbacks: at the beginning of the
>>__update method, if I do not destroy and re-create the Canvas, but simply
>>empty its contents, the script works. But it keeps the commands declared
>>created at the tcl level for the former bindings, so it silently eats up
>>memory.
>>
> 
> 
> 
> does it still eat up memory if you, for example, change the top of the 
> __update method to:
> 
>    def __update(self):
>      if not self.__tabsCanvas:  
>          self.__tabsCanvas = Canvas(self, bg=self.cget('background'),
>                                     height=self.__canvasHeight)
>          self.__tabsCanvas.grid(row=0, column=0, sticky='nswe')
>      self.__tabsCanvas.delete("all")

Yes it does: the call to delete('all') on the canvas doesn't delete the
callbacks registered at the tcl level for all the bindings done in the
canvas. These commands are only deleted when the canvas itself is deleted.

Consider the following script:

--eatmem.py---------------------------
from Tkinter import *

root = Tk()

cnv = Canvas(root)
cnv.pack()

def spam(*w): print spam

def eat():
   cnv.delete(ALL)
   l = cnv.create_line(10, 10, 10, 30)
   cnv.tag_bind(l, '<1>', spam)
   root.after(100, eat)

eat()

root.mainloop()
--------------------------------------

If you run it and monitor the size of the running process either with the
task manager on Windows or a "ps -A -o vsz -o args | grep eatmem' on Linux,
you'll notice the memory occupied by the program slowly but steadily grows
(you'll have to wait a little before it starts growing). Remove the bind, and
it doesn't happen anymore.

The same happens for commands in menus: they're only deleted when the whole
menu goes away, not when the entry having the command is deleted.

If you know something that can be done about that, I'm interested!
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list