Issue36756
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) * ![]() |
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) * ![]() |
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:14 | admin | set | github: 80937 |
| 2019-08-05 09:57:44 | taleinat | set | nosy:
+ taleinat messages: + msg349041 |
| 2019-05-02 06:30:36 | serhiy.storchaka | set | assignee: serhiy.storchaka messages: + msg341252 |
| 2019-04-30 09:24:31 | xtreak | set | nosy:
+ gpolo, serhiy.storchaka title: createcommand memory leak -> tkinter tk.createcommand memory leak |
| 2019-04-30 09:09:45 | WKraus | create | |
