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
Changes from 2 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
190 changes: 99 additions & 91 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from decimal import Decimal
from enum import Enum
from typing import Callable

from rubicon.objc import (
SEL,
Expand All @@ -32,6 +33,7 @@
NSEdgeInsets,
NSEdgeInsetsMake,
NSMakeRect,
NSMutableArray,
NSObject,
NSObjectProtocol,
NSPoint,
Expand Down Expand Up @@ -91,6 +93,26 @@ class struct_large(Structure):
_fields_ = [("x", c_char * 17)]


def assert_lifecycle(
test: unittest.TestCase, object_constructor: Callable[[], ObjCInstance]
) -> None:
obj = object_constructor()

wr = ObjcWeakref.alloc().init()
wr.weak_property = obj

with autoreleasepool():
del obj
gc.collect()

test.assertIsNotNone(
wr.weak_property,
"object was deallocated before end of autorelease pool",
)

test.assertIsNone(wr.weak_property, "object was not deallocated")


class RubiconTest(unittest.TestCase):
def test_sel_by_name(self):
self.assertEqual(SEL(b"foobar").name, b"foobar")
Expand Down Expand Up @@ -1872,128 +1894,114 @@ class TestO:
self.assertIsNone(wr_python_object())

def test_objcinstance_returned_lifecycle(self):
"""An object is retained when creating an ObjCInstance for it and autoreleased
when the ObjCInstance is garbage collected.
"""An object is retained when creating an ObjCInstance for it without implicit
ownership It is autoreleased when the ObjCInstance is garbage collected.
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
"""
with autoreleasepool():
# Return an object which we don't own. Using str(uuid) here instead of a
# common string ensure that we get have the only reference.
obj = NSString.stringWithString(str(uuid.uuid4()))

# Check that the object is retained when we create the ObjCInstance.
self.assertEqual(obj.retainCount(), 1, "object was not retained")
def create_object():
with autoreleasepool():
return NSString.stringWithString(str(uuid.uuid4()))

# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj
assert_lifecycle(self, create_object)

with autoreleasepool():
del obj
gc.collect()
def test_objcinstance_alloc_lifecycle(self):
"""We properly retain and release objects that are allocated but never
initialized."""

self.assertIsNotNone(
wr.weak_property,
"object was deallocated before end of autorelease pool",
)
def create_object():
with autoreleasepool():
return NSObject.alloc()

self.assertIsNone(wr.weak_property, "object was not deallocated")
assert_lifecycle(self, create_object)

def test_objcinstance_owned_lifecycle(self):
"""An object is not additionally retained when we create it ourselves. It is
autoreleased when the ObjCInstance is garbage collected.
def test_objcinstance_alloc_init_lifecycle(self):
"""An object is not additionally retained when we create and initialize it
through an alloc().init() chain. It is autoreleased when the ObjCInstance is
garbage collected.
"""
# Create an object which we own. Check that is has a retain count of 1.
obj = NSObject.alloc().init()

self.assertEqual(obj.retainCount(), 1, "object should be retained only once")
def create_object():
return NSObject.alloc().init()

# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj
assert_lifecycle(self, create_object)

with autoreleasepool():
del obj
gc.collect()
def test_objcinstance_new_lifecycle(self):
"""An object is not additionally retained when we create and initialize it with
a new call. It is autoreleased when the ObjCInstance is garbage collected.
"""

self.assertIsNotNone(
wr.weak_property,
"object was deallocated before end of autorelease pool",
)
def create_object():
return NSObject.new()

self.assertIsNone(wr.weak_property, "object was not deallocated")
assert_lifecycle(self, create_object)

def test_objcinstance_immutable_copy_lifecycle(self):
"""If the same object is returned from multiple creation methods, it is still
freed on Python garbage collection."""
with autoreleasepool():
obj0 = NSString.stringWithString(str(uuid.uuid4()))
obj1 = obj0.copy()
obj2 = obj0.copy()
def test_objcinstance_copy_lifecycle(self):
"""An object is not additionally retained when we create and initialize it with
a copy call. It is autoreleased when the ObjCInstance is garbage collected.
"""

self.assertIs(obj0, obj1)
self.assertIs(obj0, obj2)
def create_object():
obj = NSMutableArray.alloc().init()
copy = obj.copy()

# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj0
# Check that the copy is a new object.
self.assertIsNot(obj, copy)
self.assertNotEqual(obj.ptr.value, copy.ptr.value)

with autoreleasepool():
del obj0, obj1, obj2
gc.collect()
return copy

self.assertIsNotNone(
wr.weak_property,
"object was deallocated before end of autorelease pool",
)
assert_lifecycle(self, create_object)

self.assertIsNone(wr.weak_property, "object was not deallocated")
def test_objcinstance_mutable_copy_lifecycle(self):
"""An object is not additionally retained when we create and initialize it with
a mutableCopy call. It is autoreleased when the ObjCInstance is garbage collected.
"""

def test_objcinstance_init_change_lifecycle(self):
"""We do not leak memory if init returns a different object than it
received in alloc."""
with autoreleasepool():
obj_allocated = NSString.alloc()
obj_initialized = obj_allocated.initWithString(str(uuid.uuid4()))
def create_object():
obj = NSMutableArray.alloc().init()
copy = obj.mutableCopy()

self.assertNotEqual(obj_allocated.ptr.value, obj_initialized.ptr.value)
# Check that the copy is a new object.
self.assertIsNot(obj, copy)
self.assertNotEqual(obj.ptr.value, copy.ptr.value)

# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj_initialized
return copy

with autoreleasepool():
del obj_allocated, obj_initialized
gc.collect()
assert_lifecycle(self, create_object)

self.assertIsNotNone(
wr.weak_property,
"object was deallocated before end of autorelease pool",
)
def test_objcinstance_immutable_copy_lifecycle(self):
"""If the same object is returned from multiple creation methods, it is still
freed on Python garbage collection."""

self.assertIsNone(wr.weak_property, "object was not deallocated")
def create_object():
with autoreleasepool():
obj = NSString.stringWithString(str(uuid.uuid4()))
copy = obj.copy()

def test_objcinstance_alloc_lifecycle(self):
"""We properly retain and release objects that are allocated but never
initialized."""
with autoreleasepool():
obj_allocated = NSObject.alloc()
# Check that the copy the same object as the original.
self.assertIs(obj, copy)
self.assertEqual(obj.ptr.value, copy.ptr.value)

self.assertEqual(obj_allocated.retainCount(), 1)
return obj

# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj_allocated
assert_lifecycle(self, create_object)

with autoreleasepool():
del obj_allocated
gc.collect()
def test_objcinstance_init_change_lifecycle(self):
"""We do not leak memory if init returns a different object than it
received in alloc."""

self.assertIsNotNone(
wr.weak_property,
"object was deallocated before end of autorelease pool",
)
def create_object():
with autoreleasepool():
obj_allocated = NSString.alloc()
obj_initialized = obj_allocated.initWithString(str(uuid.uuid4()))

# Check that the initialized object is a different one than the allocated.
self.assertIsNot(obj_allocated, obj_initialized)
self.assertNotEqual(obj_allocated.ptr.value, obj_initialized.ptr.value)

return obj_initialized

self.assertIsNone(wr.weak_property, "object was not deallocated")
assert_lifecycle(self, create_object)

def test_objcinstance_init_none(self):
"""We do not segfault if init returns nil."""
Expand Down
Loading