Skip to content

Commit

Permalink
Protect against an edge case of garbage collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Oct 30, 2023
1 parent 1583545 commit ff64a99
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions cocoa/src/toga_cocoa/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ def __del__(self): # pragma: nocover
def _remove_constraints(self):
if self.container:
# print(f"Remove constraints for {self.widget} in {self.container}")
self.container.native.removeConstraint(self.width_constraint)
self.container.native.removeConstraint(self.height_constraint)
self.container.native.removeConstraint(self.left_constraint)
self.container.native.removeConstraint(self.top_constraint)
# Due to the unpredictability of garbage collection, it's possible for
# the native object of the window's container to be deleted on the ObjC
# side before the constraints for the window have been removed. Protect
# against this possibility.
if self.container.native:
self.container.native.removeConstraint(self.width_constraint)
self.container.native.removeConstraint(self.height_constraint)
self.container.native.removeConstraint(self.left_constraint)
self.container.native.removeConstraint(self.top_constraint)

self.width_constraint.release()
self.height_constraint.release()
Expand Down
1 change: 1 addition & 0 deletions cocoa/src/toga_cocoa/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(
def __del__(self):
self._min_height_constraint.release()
self._min_width_constraint.release()
self.native = None

@property
def content(self):
Expand Down

0 comments on commit ff64a99

Please sign in to comment.