This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: tkinter tk.createcommand memory leak
Type: resource usage Stage:
Components: Tkinter Versions: Python 3.7, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: WKraus, gpolo, serhiy.storchaka, taleinat
Priority: normal Keywords:

Created on 2019-04-30 09:09 by WKraus, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
tclmem_bug.py WKraus, 2019-04-30 09:09 Create memory leak with tk.createcommand
Messages (3)
msg341140 - (view) Author: Wolfram Kraus (WKraus) Date: 2019-04-30 09:09
When using tk.createcommand you get a memory leak when you don't explicitly call tk.deletecommand to remove this command.
See attached file: __del__ never get's called due to the memory leak and because of that calling tk.deletecommand inside __del__ has no effect. If you remove the tk.createcommand everything works fine.
msg341252 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-02 06:30
There are two problems: one in your code and one in tkinter or Tcl.

The problem with your code is that it creates new Tcl interpreters in loop. Create a single Tcl interpreters and create commands in a loop. This will reduce the leak to constant amount.

The problem with either the tkinter module or the Tcl interpreter is that the reference to created commands should be released when delete the command explicitly (Tcl_DeleteCommand), or create a new command with the same name (Tcl_CreateCommand), or delete the Tcl interpreter (Tcl_DeleteInterp), but for unknown cause this doesn't happen in the last case.
msg349041 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2019-08-05 09:57
Tkinter calls Tcl_DeleteInterp when a Tk object is garbage collected, and it registers a cleanup callback for each registered command, which according to the Tcl docs should be called upon Tcl_DeleteInterp[1]. So this must either be a bug in Tcl or something in the circumstances isn't giving it a chance to clean up the commands.

It's worth noting that Tk.destroy() calls Misc.destroy() which explicitly calls deletecommand for all registered commands. So calling .destroy() when done with a Tk instance, which is good practice in general, will also avoid this issue.

Considering the above, I'm not sure this is worth investigating and addressing...

A simple solution could be to add __del__ to Tk or Misc and have that also clean up any registered commands.
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 80937
2019-08-05 09:57:44taleinatsetnosy: + taleinat
messages: + msg349041
2019-05-02 06:30:36serhiy.storchakasetassignee: serhiy.storchaka
messages: + msg341252
2019-04-30 09:24:31xtreaksetnosy: + gpolo, serhiy.storchaka

title: createcommand memory leak -> tkinter tk.createcommand memory leak
2019-04-30 09:09:45WKrauscreate