Skip to content

Commit

Permalink
No longer store strong references to parent object in guiHelper.BoxSi…
Browse files Browse the repository at this point in the history
…zerHelper to decrease likelihood of circular references in NVDA's GUI (#16079)

Related to #16019

Summary of the issue:
BoxSizerHelper class stores a strong reference to its parent. When it is mistakenly assigned to a member of its parent a circular reference is created making it impossible for Python's garbage collector to destroy the dialog when it goes out of scope.

Description of user facing changes
Should not be noticeable.

Description of development approach
BoxSizerHelper now stores a weak reference to its parent.
  • Loading branch information
lukaszgo1 committed Jan 23, 2024
1 parent 6740805 commit 1c59ac2
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions source/gui/guiHelper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: UTF-8 -*-
# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2016-2023 NV Access Limited
# Copyright (C) 2016-2024 NV Access Limited, Łukasz Golonka
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.

Expand Down Expand Up @@ -44,6 +43,7 @@ def __init__(self, parent):
...
"""
from contextlib import contextmanager
import weakref
from typing import (
Generic,
Optional,
Expand Down Expand Up @@ -314,7 +314,7 @@ def __init__(
@type orientation: wx.HORIZONTAL or wx.VERTICAL
@param sizer: the sizer to use rather than constructing one.
"""
self._parent = parent
self._parentRef = weakref.ref(parent)
self.hasFirstItemBeenAdded = False
if orientation and sizer:
raise ValueError("Supply either orientation OR sizer. Not both.")
Expand Down Expand Up @@ -384,7 +384,7 @@ def addLabeledControl(
Relies on guiHelper.LabeledControlHelper and thus guiHelper.associateElements, and therefore inherits any
limitations from there.
"""
parent = self._parent
parent = self._parentRef()
if isinstance(self.sizer, wx.StaticBoxSizer):
parent = self.sizer.GetStaticBox()
labeledControl = LabeledControlHelper(parent, labelText, wxCtrlClass, **kwargs)
Expand Down Expand Up @@ -422,11 +422,11 @@ def addDialogDismissButtons(
elif isinstance(buttons, (wx.Sizer, wx.Button)):
toAdd = buttons
elif isinstance(buttons, int):
toAdd = self._parent.CreateButtonSizer(buttons)
toAdd = self._parentRef().CreateButtonSizer(buttons)
else:
raise NotImplementedError("Unknown type: {}".format(buttons))
if separated:
parentBox = self._parent
parentBox = self._parentRef()
if isinstance(self.sizer, wx.StaticBoxSizer):
parentBox = self.sizer.GetStaticBox()
self.addItem(wx.StaticLine(parentBox), flag=wx.EXPAND)
Expand Down

0 comments on commit 1c59ac2

Please sign in to comment.