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

Updated memory management #543

Merged
merged 66 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
9abcd0d
Retain on ObjCInstance creation, autorelease on __del__
samschott Nov 19, 2024
6605842
update tests
samschott Nov 19, 2024
931c352
add change note
samschott Nov 19, 2024
a618f2a
use autorelease instead of release in __del__
samschott Nov 20, 2024
b1bf61c
code formatting
samschott Nov 20, 2024
21f2e0b
update docs
samschott Nov 20, 2024
20ab8f9
add comment about autorelease vs release
samschott Nov 23, 2024
160c819
remove now unneeded cache staleness check
samschott Nov 23, 2024
ce9d78c
remove stale instance cache tests
samschott Nov 23, 2024
6d89330
update test_objcinstance_dealloc
samschott Nov 24, 2024
3bb7ccc
correct inline comment
samschott Nov 24, 2024
c0b091c
make returned_from_method private
samschott Nov 24, 2024
ab1f762
update ObjCInstance doc string
samschott Nov 24, 2024
544d694
updated docs
samschott Nov 24, 2024
b4a1624
update spellchecker
samschott Nov 24, 2024
f0edb5b
update change notes with migration instructions
samschott Nov 24, 2024
22396dc
Rephrase removal note
samschott Nov 25, 2024
7d51fde
remove unneeded space in doc string
samschott Nov 25, 2024
acfa546
change bugfix to feature note
samschott Nov 25, 2024
18e08cc
Fix incorrect inline comment
samschott Nov 25, 2024
532fbe0
trim trailing whitespace
samschott Nov 25, 2024
52e92c0
update test comment
samschott Nov 25, 2024
efed734
check that objects are not deallocated before end of autorelease pool
samschott Nov 25, 2024
ab8a895
merge object lifecycle tests
samschott Nov 25, 2024
30e4277
add a test case for copyWithZone returning the existing instance with…
samschott Nov 25, 2024
c3a4fe1
release additional refcounts by copy calls on the same ObjCInstance
samschott Nov 25, 2024
7bdc31f
rewrite the copy lifecycle test to use NSDictionary instead of a cust…
samschott Nov 26, 2024
460728b
prevent errors on ObjCInstance garbage collection when `send_message`…
samschott Nov 26, 2024
d9c0f62
switch copy lifecycle test to use NSString
samschott Nov 26, 2024
49d9381
remove unused import
samschott Nov 26, 2024
e0d7792
fix spelling mistake
samschott Nov 26, 2024
715912f
spelling updates
samschott Nov 26, 2024
20e45b6
spelling updates
samschott Nov 26, 2024
86b29a4
spelling updates
samschott Nov 26, 2024
944328d
black code formatting
samschott Nov 26, 2024
3b88aaa
rename test case to "immutable copy lifecycle"
samschott Nov 26, 2024
58d0276
improve inline docs
samschott Nov 27, 2024
84e3a9f
special handling for init
samschott Nov 27, 2024
ab46b9d
add tests for init object change
samschott Nov 28, 2024
2305122
implement proper method family detection
samschott Nov 28, 2024
2e4eccb
ensure partial methods are loaded from all superclasses
samschott Nov 28, 2024
2989540
remove unneeded whitespace
samschott Nov 29, 2024
0bc749c
improved release-on-cache-hit documentation
samschott Nov 29, 2024
04981e3
updated change notes
samschott Nov 29, 2024
54ed55c
add test for get_method_family
samschott Nov 29, 2024
c6096c2
remove loop that breaks early on method loading
samschott Nov 29, 2024
a28c901
make method loading slightly clearer
samschott Nov 29, 2024
1b306e2
extract and document method name to tuple logic
samschott Nov 29, 2024
849749e
fall back to full method usage if partial method lookup fails
samschott Nov 29, 2024
15593c1
update partial method cache after successful lookup
samschott Nov 29, 2024
39c548e
Revert "remove loop that breaks early on method loading"
samschott Nov 30, 2024
b78c41d
Revert "ensure partial methods are loaded from all superclasses"
samschott Nov 30, 2024
cb996ad
Reapply "ensure partial methods are loaded from all superclasses"
samschott Nov 30, 2024
6cf88a5
centralize logic for method family
samschott Dec 2, 2024
aa48b5d
update test description
samschott Dec 4, 2024
7fa9e71
update inline comments
samschott Dec 4, 2024
3be2190
fix method family detection
samschott Dec 4, 2024
dded73e
add test case for alloc without init
samschott Dec 4, 2024
199f807
race free interpreter shutdown handling
samschott Dec 4, 2024
d0961b4
black formatting
samschott Dec 4, 2024
a8e9193
more precise family determination to follow the exact rules laid out …
samschott Dec 4, 2024
296cf83
update tests for method family determination to check for non-lowerca…
samschott Dec 4, 2024
93300f3
fix typo in method_name_to_tuple doc string
samschott Dec 4, 2024
2be945b
more exhaustive refcount tests
samschott Dec 5, 2024
2387c02
remove duplicate code from test_objcinstance_returned_lifecycle
samschott Dec 5, 2024
51ba75b
Fix typo
mhsmith Dec 5, 2024
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
2 changes: 2 additions & 0 deletions changes/256.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Retain Objective-C objects when creating Python wrappers and release them when the
Python wrapped is garbage collected.
samschott marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 8 additions & 7 deletions docs/how-to/memory-management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ Rubicon Objective-C operates at runtime, it cannot make use of ARC.
Reference counting in Rubicon Objective-C
-----------------------------------------

You won't have to manage reference counts in Python, Rubicon Objective-C will do
that work for you. It does so by tracking when you gain ownership of an object.
This is the case when you create an Objective-C instance using a method whose
name begins with ``alloc``, ``new``, ``copy``, or ``mutableCopy``. Rubicon
Objective-C will then insert a ``release`` call when the Python variable that
corresponds to the Objective-C instance is deallocated.
In most cases, you won't have to manage reference counts in Python, Rubicon
Objective-C will do that work for you. It does so by calling ``retain`` on an
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
object when Rubicon creates a ``ObjCInstance`` for it on the Python side, and calling
``autorelease`` when the ``ObjCInstance`` is garbage collected in Python. Retaining
the object ensures it is not deallocated while it is still referenced from Python
and releasing it again on ``__del__`` ensures that we do not leak memory.

An exception to this is when you manually ``retain`` an object. Rubicon
Objective-C will not keep track of such retain calls and you will be
responsible to insert appropriate ``release`` calls yourself.
responsible to insert appropriate ``release`` or ``autorelease`` calls yourself
to prevent leaking memory.

You will also need to pay attention to reference counting in case of **weak
references**. In Objective-C, creating a **weak reference** means that the
Expand Down
145 changes: 23 additions & 122 deletions src/rubicon/objc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,7 @@ def __call__(self, receiver, *args, convert_args=True, convert_result=True):

# Convert result to python type if it is an instance or class pointer.
if self.restype is not None and issubclass(self.restype, objc_id):
result = ObjCInstance(result)

# Mark for release if we acquire ownership of an object. Do not autorelease here because
# we might retain a Python reference while the Obj-C reference goes out of scope.
if self.name.startswith((b"alloc", b"new", b"copy", b"mutableCopy")):
result._needs_release = True
result = ObjCInstance(result, returned_from_method=self.name)

return result

Expand Down Expand Up @@ -783,29 +778,13 @@ def objc_class(self):
return super(ObjCInstance, type(self)).__getattribute__(self, "_objc_class")
except AttributeError:
# This assumes that objects never change their class after they are
# seen by Rubicon. There are two reasons why this may not be true:
#
# 1. Objective-C runtime provides a function object_setClass that
# can change an object's class after creation, and some code
# manipulates objects' isa pointers directly (although the latter
# is no longer officially supported by Apple). This is not
# commonly done in practice, and even then it is usually only
# done during object creation/initialization, so it's basically
# safe to assume that an object's class will never change after
# it's been wrapped in an ObjCInstance.
# 2. If a memory address is freed by the Objective-C runtime, and
# then re-allocated by an object of a different type, but the
# Python ObjCInstance wrapper persists, Python will assume the
# object is still of the old type. If a new ObjCInstance wrapper
# for the same pointer is re-created, a check is performed to
# ensure the type hasn't changed; this problem only affects
# pre-existing Python wrappers. If this occurs, it probably
# indicates an issue with the retain count on the Python side (as
# the Objective-C runtime shouldn't be able to dispose of an
# object if Python still has a handle to it). If this *does*
# happen, it will manifest as objects appearing to be the wrong
# type, and/or objects having the wrong list of attributes
# available. Refs #249.
# seen by Rubicon. This can occur because the Objective-C runtime provides a
# function object_setClass that can change an object's class after creation,
# and some code manipulates objects' isa pointers directly (although the
# latter is no longer officially supported by Apple). This is not commonly
# done in practice, and even then it is usually only done during object
# creation/initialization, so it's basically safe to assume that an object's
# class will never change after it's been wrapped in an ObjCInstance.
super(ObjCInstance, type(self)).__setattr__(
self, "_objc_class", ObjCClass(libobjc.object_getClass(self))
)
Expand All @@ -815,7 +794,9 @@ def objc_class(self):
def _associated_attr_key_for_name(name):
return SEL(f"rubicon.objc.py_attr.{name}")

def __new__(cls, object_ptr, _name=None, _bases=None, _ns=None):
def __new__(
cls, object_ptr, _name=None, _bases=None, _ns=None, returned_from_method=b""
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
):
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
"""The constructor accepts an :class:`~rubicon.objc.runtime.objc_id` or
anything that can be cast to one, such as a :class:`~ctypes.c_void_p`,
or an existing :class:`ObjCInstance`.
Expand Down Expand Up @@ -852,68 +833,17 @@ class or a metaclass, an instance of :class:`ObjCClass` or
# If an ObjCInstance already exists for the Objective-C object,
# reuse it instead of creating a second ObjCInstance for the
# same object.
cached = cls._cached_objects[object_ptr.value]

# In a high-churn environment, it is possible for an object to
# be deallocated, and the same memory address be re-used on the
# Objective-C side, but the Python wrapper object for the
# original instance has *not* been cleaned up. In that
# situation, an attempt to wrap the *new* Objective-C object
# instance will cause a false positive cache hit; returning a
# Python object that has a class that doesn't match the class of
# the new instance.
#
# To prevent this, when we get a cache hit on an ObjCInstance,
# use the raw Objective-C API on the pointer to get the current
# class of the object referred to by the pointer. If there's a
# discrepancy, purge the cache for the memory address, and
# re-create the object.
#
# We do this both when the type *is* ObjCInstance (the case when
# instantiating a literal ObjCInstance()), and when type is an
# ObjCClass instance (e.g., ObjClass("Example"), which is the
# type of a directly instantiated instance of Example.
#
# We *don't* do this when the type *is* ObjCClass,
# ObjCMetaClass, as there's a race condition on startup -
# retrieving `.objc_class` causes the creation of ObjCClass
# objects, which will cause cache hits trying to re-use existing
# ObjCClass objects. However, ObjCClass instances generally
# won't be recycled or reused, so that should be safe to exclude
# from the cache freshness check.
#
# One edge case with this approach: if the old and new
# Objective-C objects have the same class, they won't be
# identified as a stale object, and they'll re-use the same
# Python wrapper. This effectively means id(obj) isn't a
# reliable instance identifier... but (a) this won't be a common
# case; (b) this flaw exists in pure Python and Objective-C as
# well, because default object identity is tied to memory
# allocation; and (c) the stale wrapper will *work*, because
# it's the correct class.
#
# Refs #249.
if cls == ObjCInstance or isinstance(cls, ObjCInstance):
cached_class_name = cached.objc_class.name
current_class_name = libobjc.class_getName(
libobjc.object_getClass(object_ptr)
).decode("utf-8")
if (
current_class_name != cached_class_name
and not current_class_name.endswith(f"_{cached_class_name}")
):
# There has been a cache hit, but the object is a
# different class, treat this as a cache miss. We don't
# *just* look for an *exact* class name match, because
# some Cocoa/UIKit classes undergo a class name change
# between `alloc()` and `init()` (e.g., `NSWindow`
# becomes `NSKVONotifying_NSWindow`). Refs #257.
raise KeyError(object_ptr.value)

return cached
return cls._cached_objects[object_ptr.value]
except KeyError:
pass

# Explicitly retain the instance on first handover to Python unless we
# received it from a method that gives us ownership already.
if not returned_from_method.startswith(
(b"alloc", b"new", b"copy", b"mutableCopy")
):
send_message(object_ptr, "retain", restype=objc_id, argtypes=[])

# If the given pointer points to a class, return an ObjCClass instead (if we're not already creating one).
if not issubclass(cls, ObjCClass) and object_isClass(object_ptr):
return ObjCClass(object_ptr)
Expand All @@ -932,7 +862,6 @@ class or a metaclass, an instance of :class:`ObjCClass` or
super(ObjCInstance, type(self)).__setattr__(
self, "_as_parameter_", object_ptr
)
super(ObjCInstance, type(self)).__setattr__(self, "_needs_release", False)
if isinstance(object_ptr, objc_block):
super(ObjCInstance, type(self)).__setattr__(
self, "block", ObjCBlock(object_ptr)
Expand All @@ -944,39 +873,11 @@ class or a metaclass, an instance of :class:`ObjCClass` or

return self

def release(self):
"""Manually decrement the reference count of the corresponding objc
object.

The objc object is sent a dealloc message when its reference
count reaches 0. Calling this method manually should not be
necessary, unless the object was explicitly ``retain``\\ed
before. Objects returned from ``.alloc().init...(...)`` and
similar calls are released automatically by Rubicon when the
corresponding Python object is deallocated.
"""
self._needs_release = False
send_message(self, "release", restype=objc_id, argtypes=[])

def autorelease(self):
"""Decrements the receiver’s reference count at the end of the current
autorelease pool block.

The objc object is sent a dealloc message when its reference
count reaches 0. If called, the object will not be released when
the Python object is deallocated.
"""
self._needs_release = False
result = send_message(self, "autorelease", restype=objc_id, argtypes=[])
return ObjCInstance(result)

def __del__(self):
"""Release the corresponding objc instance if we own it, i.e., if it
was returned by a method starting with :meth:`alloc`, :meth:`new`,
:meth:`copy`, or :meth:`mutableCopy` and it wasn't already explicitly
released by calling :meth:`release` or :meth:`autorelease`."""
if self._needs_release:
send_message(self, "release", restype=objc_id, argtypes=[])
# Autorelease our reference on garbage collection of the Python wrapper. We use
# release instead of autorelease allow ObjC to take ownership of an object when
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
# it is passed for example to a strong property.
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
send_message(self, "autorelease", restype=objc_id, argtypes=[])

def __str__(self):
"""Get a human-readable representation of ``self``.
Expand Down
Loading