Skip to content

Commit

Permalink
Merge pull request #2472 from samschott/fix-image-memleak
Browse files Browse the repository at this point in the history
Fix memory leak for toga.Icon and toga.Image on Cocoa
  • Loading branch information
freakboy3742 authored Apr 2, 2024
2 parents 4a27e6e + 9540a4e commit eb151e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
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.
22 changes: 11 additions & 11 deletions cocoa/src/toga_cocoa/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +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:
# Calling `release` here disabled Rubicon's "release on delete" automation.
# We therefore add an explicit `release` call in __del__ if the NSImage was
# initialized successfully.
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):
if self.native:
self.native.release()
Expand Down
27 changes: 20 additions & 7 deletions cocoa/src/toga_cocoa/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,44 @@ 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:
# Calling `release` here disabled Rubicon's "release on delete" automation.
# We therefore add an explicit `release` call in __del__ if the NSImage was
# initialized successfully.
image.release()

def __del__(self):
if self._needs_release:
self.native.release()

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

Expand Down

0 comments on commit eb151e8

Please sign in to comment.