Skip to content

tkinter: avoid reference loops with Variables and Fonts #66266

Closed

Description

@serhiy-storchaka
BPO 22068
Nosy @terryjreedy, @kbkaiser, @vstinner, @serwy, @serhiy-storchaka
Files
  • tracemalloc.txt
  • regrtest_tracemalloc.patch
  • tkinter_refloops-2.7.patch
  • 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 = <Date 2014-08-17.13:47:14.536>
    created_at = <Date 2014-07-25.15:34:59.628>
    labels = ['type-bug', 'expert-tkinter']
    title = 'tkinter: avoid reference loops with Variables and Fonts'
    updated_at = <Date 2014-08-17.13:47:14.536>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2014-08-17.13:47:14.536>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2014-08-17.13:47:14.536>
    closer = 'serhiy.storchaka'
    components = ['Tkinter']
    creation = <Date 2014-07-25.15:34:59.628>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['36087', '36088', '36105']
    hgrepos = []
    issue_num = 22068
    keywords = ['patch']
    message_count = 12.0
    messages = ['223958', '223960', '223961', '223963', '224021', '224033', '224042', '224092', '224361', '224363', '225273', '225440']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'kbk', 'vstinner', 'roger.serwy', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue22068'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

    Activity

    1. serhiy-storchaka commented on Jul 25, 2014

      @serhiy-storchaka
      MemberAuthor
      $ ./python -m test.regrtest -ugui -v test_idle test_gc
      ...

      ======================================================================
      FAIL: test_saveall (test.test_gc.GCTests)
      ----------------------------------------------------------------------

      Traceback (most recent call last):
        File "/home/serhiy/py/cpython-2.7/Lib/test/test_gc.py", line 200, in test_saveall
          self.assertEqual(gc.garbage, [])
      AssertionError: Lists differ: [<Tkinter.StringVar instance a... != []

      First list contains 24 additional elements.
      First extra element 0:
      PY_VAR0

      Diff is 1123 characters long. Set self.maxDiff to None to see it.

      ----------------------------------------------------------------------

    2. vstinner commented on Jul 25, 2014

      @vstinner
      Member

      It looks like test_idle leaks uncollectable objects.

      I modified regrtest to use tracemalloc, I attach the output: tracemalloc.txt. Good luck to find the leaks ;-)

    3. vstinner commented on Jul 25, 2014

      @vstinner
      Member

      regrtest_tracemalloc.patch: my patch for regrtest.py to dump the traceback where garbage objects where allocated using tracemalloc.
      http://pytracemalloc.readthedocs.org/

      (Note: you need to recompile Python to use tracemalloc on Python < 3.4.)

    4. serhiy-storchaka commented on Jul 25, 2014

      @serhiy-storchaka
      MemberAuthor

      Indeed, there are a lot of small reference loops in ConfigDialog. Tk variables save reference to the dialog and the dialog saves references to variables. Either variables should be created with different argument (i.e. self.parent), or they should be deleted when ConfigDialog is destroyed.

    5. terryjreedy commented on Jul 26, 2014

      @terryjreedy
      Member

      ConfigDialog is a good guess as I added a minimal test this month.
      I will try to revise to not create loops in the first place.

    6. serhiy-storchaka commented on Jul 26, 2014

      @serhiy-storchaka
      MemberAuthor

      Here is a patch against 2.7 which get rid of reference loops in Tk variables and Font. This will fix not only ConfigDialog, but any similar user code.

      In 3.4+ such reference loops are successfully resolved, but I think we should foreport this path to 3.4+ because it also fixes other minor bug: callbacks registered to trace variable now live while the variable lives, not while widget lives.

    7. vstinner commented on Jul 26, 2014

      @vstinner
      Member

      I agree that the patch shoukd also br applied to 3.4.

    8. terryjreedy commented on Jul 27, 2014

      @terryjreedy
      Member

      F:\Python\dev>2\py27\pcbuild\python_d.exe -m test.regrtest -R :: -uall test_idle
      test_idle leaked [1945, 1945, 1945, 1945] references, sum=7780

      There are none with 3.4, so the new gc is doing its job.
      There are also none with test_configdialog renamed xtest... ,

      GetCfgSectionNameDialog.__init__ in configSectionNameDialog.py saves self.parent for later use as the parent for XyxVars. I am looking at doing the same for config dialog.

    9. changed the title [-]test_gc fails after test_idle[/-] [+]test_idle leaks uncollectable objects[/+] on Jul 29, 2014
    10. python-dev commented on Jul 30, 2014

      python-devmannequin
      Mannequin

      New changeset 6b7f189daa62 by Terry Jan Reedy in branch '2.7':
      Issue bpo-22068: Don't create self reference cycles in idlelib.ConfigDialog.
      http://hg.python.org/cpython/rev/6b7f189daa62

      New changeset 1927f47a1838 by Terry Jan Reedy in branch '3.4':
      Issue bpo-22068: Don't create self reference cycles in idlelib.ConfigDialog.
      http://hg.python.org/cpython/rev/1927f47a1838

    11. terryjreedy commented on Jul 30, 2014

      @terryjreedy
      Member

      -R no longer finds leaks with current Idle tests. There might still be some in modules not tested.

    12. changed the title [-]test_idle leaks uncollectable objects[/-] [+]tkinter: avoid reference loops with Variables and Fonts[/+] on Jul 30, 2014
    13. serhiy-storchaka commented on Aug 13, 2014

      @serhiy-storchaka
      MemberAuthor

      If there are no objections I'll commit the patch.

    14. python-dev commented on Aug 17, 2014

      python-devmannequin
      Mannequin

      New changeset 0b79c702abda by Serhiy Storchaka in branch '2.7':
      Issue bpo-22068: Avoided reference loops with Variables and Fonts in Tkinter.
      http://hg.python.org/cpython/rev/0b79c702abda

      New changeset 873002eb8087 by Serhiy Storchaka in branch '3.4':
      Issue bpo-22068: Avoided reference loops with Variables and Fonts in Tkinter.
      http://hg.python.org/cpython/rev/873002eb8087

      New changeset f44f5daff665 by Serhiy Storchaka in branch 'default':
      Issue bpo-22068: Avoided reference loops with Variables and Fonts in Tkinter.
      http://hg.python.org/cpython/rev/f44f5daff665

    15. transferred this issue fromon Apr 10, 2022
    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