Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak for toga.Icon and toga.Image on Cocoa #2472

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2472.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix memory leaks for toga.Icon and toga.Image in the Cocoa backend.
21 changes: 10 additions & 11 deletions cocoa/src/toga_cocoa/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@ def __init__(self, interface, path):
self.path = path
try:
# We *should* be able to do a direct NSImage.alloc.init...(), but if the
# image file is invalid, the init fails, and returns NULL - but we've
# created an ObjC instance, so when the object passes out of scope, Rubicon
# tries to free it, which segfaults. To avoid this, we retain result of the
# alloc() (overriding the default Rubicon behavior of alloc), then release
# that reference once we're done. If the image was created successfully, we
# temporarily have a reference count that is 1 higher than it needs to be;
# if it fails, we don't end up with a stray release.
# image file is invalid, the init fails, returns NULL, and releases the
# Objective-C object. Since we've created an ObjC instance, when the object
# passes out of scope, Rubicon tries to free it, which segfaults.
# To avoid this, we retain result of the alloc() (overriding the default
# Rubicon behavior of alloc), then release that reference once we're done.
# If the image was created successfully, we temporarily have a reference
# count that is 1 higher than it needs to be; if it fails, we don't end up
# with a stray release.
image = NSImage.alloc().retain()
self.native = image.initWithContentsOfFile(str(path))
if self.native is None:
raise ValueError(f"Unable to load icon from {path}")
finally:
image.release()

# Multiple icon interface instances can end up referencing the same native
# instance, so make sure we retain a reference count at the impl level.
self.native.retain()

def __del__(self):
# Calling `release` during init disabled Rubicon's "release on delete"
# automation. We therefore need to release manually here.
freakboy3742 marked this conversation as resolved.
Show resolved Hide resolved
if self.native:
self.native.release()

Expand Down
26 changes: 19 additions & 7 deletions cocoa/src/toga_cocoa/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,43 @@ class Image:

def __init__(self, interface, path=None, data=None, raw=None):
self.interface = interface
self._needs_release = False

try:
# We *should* be able to do a direct NSImage.alloc.init...(), but if the
# image file is invalid, the init fails, and returns NULL - but we've
# created an ObjC instance, so when the object passes out of scope, Rubicon
# tries to free it, which segfaults. To avoid this, we retain result of the
# alloc() (overriding the default Rubicon behavior of alloc), then release
# that reference once we're done. If the image was created successfully, we
# temporarily have a reference count that is 1 higher than it needs to be;
# if it fails, we don't end up with a stray release.
# image file is invalid, the init fails, returns NULL, and releases the
# Objective-C object. Since we've created an ObjC instance, when the object
# passes out of scope, Rubicon tries to free it, which segfaults.
# To avoid this, we retain result of the alloc() (overriding the default
# Rubicon behavior of alloc), then release that reference once we're done.
# If the image was created successfully, we temporarily have a reference
# count that is 1 higher than it needs to be; if it fails, we don't end up
# with a stray release.
image = NSImage.alloc().retain()
if path:
self.native = image.initWithContentsOfFile(str(path))
if self.native is None:
raise ValueError(f"Unable to load image from {path}")
else:
self._needs_release = True
elif data:
nsdata = NSData.dataWithBytes(data, length=len(data))
self.native = image.initWithData(nsdata)
if self.native is None:
raise ValueError("Unable to load image from data")
else:
self._needs_release = True
else:
self.native = raw
finally:
image.release()

def __del__(self):
# Calling `release` during init disabled Rubicon's "release on delete"
# automation. We therefore need to release manually here.
if self._needs_release:
self.native.release()

def get_width(self):
return self.native.size.width

Expand Down
Loading