python / cpython

The Python programming language
https://www.python.org
Other
63.4k stars 30.36k forks source link

tkinter tk.createcommand memory leak #80937

Open 5c331fbe-0123-4cc3-a6e1-246955862919 opened 5 years ago

5c331fbe-0123-4cc3-a6e1-246955862919 commented 5 years ago
BPO 36756
Nosy @taleinat, @serhiy-storchaka
Files
  • tclmem_bug.py: Create memory leak with tk.createcommand
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = 'https://github.com/serhiy-storchaka' closed_at = None created_at = labels = ['3.7', 'expert-tkinter', 'performance'] title = 'tkinter tk.createcommand memory leak' updated_at = user = 'https://bugs.python.org/WKraus' ``` bugs.python.org fields: ```python activity = actor = 'taleinat' assignee = 'serhiy.storchaka' closed = False closed_date = None closer = None components = ['Tkinter'] creation = creator = 'WKraus' dependencies = [] files = ['48292'] hgrepos = [] issue_num = 36756 keywords = [] message_count = 3.0 messages = ['341140', '341252', '349041'] nosy_count = 4.0 nosy_names = ['taleinat', 'gpolo', 'serhiy.storchaka', 'WKraus'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'resource usage' url = 'https://bugs.python.org/issue36756' versions = ['Python 2.7', 'Python 3.7'] ```

    5c331fbe-0123-4cc3-a6e1-246955862919 commented 5 years ago

    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.

    serhiy-storchaka commented 5 years ago

    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.

    taleinat commented 5 years ago

    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.