Skip to content

Commit

Permalink
prevent duplicate release of NSImage
Browse files Browse the repository at this point in the history
  • Loading branch information
samschott committed Nov 23, 2024
1 parent 596ca0f commit bc35c3a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 58 deletions.
37 changes: 14 additions & 23 deletions cocoa/src/toga_cocoa/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,20 @@ def __init__(self, interface, path):
else:
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, 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()

def __del__(self):
if self.native:
self.native.release()
# We *should* be able to do a direct NSImage.alloc.init...(), but if the
# 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}")
image.release()

def _as_size(self, size):
image = self.native.copy()
Expand Down
57 changes: 22 additions & 35 deletions cocoa/src/toga_cocoa/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,30 @@ 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, 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()
# We *should* be able to do a direct NSImage.alloc.init...(), but if the
# 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}")
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.native = raw

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

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

0 comments on commit bc35c3a

Please sign in to comment.