Skip to content

tkinter tk.createcommand memory leak #80937

Closed

Description

@WKraus
mannequin
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:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = None
    created_at = <Date 2019-04-30.09:09:45.057>
    labels = ['3.7', 'expert-tkinter', 'performance']
    title = 'tkinter tk.createcommand memory leak'
    updated_at = <Date 2019-08-05.09:57:44.451>
    user = 'https://bugs.python.org/WKraus'

    bugs.python.org fields:

    activity = <Date 2019-08-05.09:57:44.451>
    actor = 'taleinat'
    assignee = 'serhiy.storchaka'
    closed = False
    closed_date = None
    closer = None
    components = ['Tkinter']
    creation = <Date 2019-04-30.09:09:45.057>
    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']

    Linked PRs

    Activity

    1. WKraus commented on Apr 30, 2019

      WKrausmannequin
      MannequinAuthor

      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.

    2. changed the title [-]createcommand memory leak[/-] [+]tkinter tk.createcommand memory leak[/+] on Apr 30, 2019
    3. serhiy-storchaka commented on May 2, 2019

      @serhiy-storchaka
      Member

      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.

    4. taleinat commented on Aug 5, 2019

      @taleinat
      Contributor

      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.

    5. transferred this issue fromon Apr 10, 2022
    6. added
      type-bugAn unexpected behavior, bug, or error
      and removed
      performancePerformance or resource usage
      on Aug 17, 2022
    7. added a commit that references this issue on Jun 26, 2026
    8. added 3 commits that reference this issue on Jun 26, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions