[Tkinter-discuss] Avoiding memory leaks using tk.call

Russell E. Owen rowen at cesmail.net
Thu Jul 7 22:53:04 CEST 2005

I'm writing code that retrieves URLs to files on disk using tcl's http 
module. (Why bother? See P.S.)

So far, so good, but I'm a bit worried I may be leaking memory.

Thus I open the output file and start the transfer with:
self._tclFile = self._tkApp.call('open', self.toPath, mode) mode,))
self._tclHTTPConn = self._tkApp.call(
                '::http::geturl', self.url,
                '-channel', self._tclFile,
                '-command', self._tclHTTPDoneCallback,
                '-progress', self._tclHTTPProgressCallback,
                '-binary', self.isBinary,
                '-timeout', self.timeLimMS

And clean up afterwards using:
self._tclHTTPConn != None:
            self._tkApp.call("::http::cleanup", self._tclHTTPConn)
            self._tclHTTPConn = None
            self._tkApp.call("close", self._tclFile)
            self._tclFile = None
and I delete the callbacks from tcl using self._tkApp.deletecommand.

It works, but I'm wondering if I'm leaking the reference to the file or 
http connection that was created inside tcl. If so, any hints on how to 
clean those up would be much appreciated.

-- Russell

P.S. why bother:
- I don't want my users to have to install Twisted framework. Though I 
get closer to that every time something like this comes up.
- I've tried using the built in python libraries for stuff like this, 
but it's a hassle due to no integration with the tk event loop. The 
resulting code does work, but doing without blocking is painful as is 
keeping track of state. Plus I've had reports of occasional failures 
that I can't reproduce on my computer.

P.P.S. The resulting code will be released in an update to my free RO 
package <http://www.astro.washington.edu/rowen/ROPython.html>. The 
package already includes tk sockets and lots of other goodies.

More information about the Tkinter-discuss mailing list