Skip to content

Commit 99e7ae0

Browse files
authored
Merge pull request #543 from samschott/updated-memory-management
Updated memory management
2 parents f8199b1 + 51ba75b commit 99e7ae0

File tree

6 files changed

+360
-299
lines changed

6 files changed

+360
-299
lines changed

changes/256.feature.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Retain Objective-C objects when creating Python wrappers and release them when the
2+
Python wrapped is garbage collected. This means that manual ``retain`` calls and
3+
subsequent ``release`` or ``autorelease`` calls from Python are no longer needed with
4+
very few exceptions, for example when writing implementations of ``copy`` that return an
5+
existing object.

changes/256.removal.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Manual calls to ``release`` or ``autorelease`` no longer cause Rubicon
2+
to skip releasing an Objective-C object when its Python wrapper is
3+
garbage collected. This means that fewer ``retain`` than ``release`` calls will cause
4+
segfaults on garbage collection. Review your code carefully for unbalanced ``retain``
5+
and ``release`` calls before updating.

docs/how-to/memory-management.rst

+27-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
Memory management for Objective-C instances
33
===========================================
44

5+
Reference counting in Objective-C
6+
=================================
7+
58
Reference counting works differently in Objective-C compared to Python. Python
69
will automatically track where variables are referenced and free memory when
710
the reference count drops to zero whereas Objective-C uses explicit reference
@@ -13,28 +16,36 @@ When enabling automatic reference counting (ARC), the appropriate calls for
1316
memory management will be inserted for you at compile-time. However, since
1417
Rubicon Objective-C operates at runtime, it cannot make use of ARC.
1518

16-
Reference counting in Rubicon Objective-C
17-
-----------------------------------------
19+
Reference management in Rubicon
20+
===============================
21+
22+
In most cases, you won't have to manage reference counts in Python, Rubicon
23+
Objective-C will do that work for you. It does so by calling ``retain`` on an
24+
object when Rubicon creates a ``ObjCInstance`` for it on the Python side, and calling
25+
``autorelease`` when the ``ObjCInstance`` is garbage collected in Python. Retaining
26+
the object ensures it is not deallocated while it is still referenced from Python
27+
and releasing it again on ``__del__`` ensures that we do not leak memory.
28+
29+
The only exception to this is when you create an object -- which is always done
30+
through methods starting with "alloc", "new", "copy", or "mutableCopy". Rubicon does
31+
not explicitly retain such objects because we own objects created by us, but Rubicon
32+
does autorelease them when the Python wrapper is garbage collected.
1833

19-
You won't have to manage reference counts in Python, Rubicon Objective-C will do
20-
that work for you. It does so by tracking when you gain ownership of an object.
21-
This is the case when you create an Objective-C instance using a method whose
22-
name begins with ``alloc``, ``new``, ``copy``, or ``mutableCopy``. Rubicon
23-
Objective-C will then insert a ``release`` call when the Python variable that
24-
corresponds to the Objective-C instance is deallocated.
34+
Rubicon Objective-C will not keep track if you additionally manually ``retain`` an
35+
object. You will be responsible to insert appropriate ``release`` or ``autorelease``
36+
calls yourself to prevent leaking memory.
2537

26-
An exception to this is when you manually ``retain`` an object. Rubicon
27-
Objective-C will not keep track of such retain calls and you will be
28-
responsible to insert appropriate ``release`` calls yourself.
38+
Weak references in Objective-C
39+
------------------------------
2940

30-
You will also need to pay attention to reference counting in case of **weak
31-
references**. In Objective-C, creating a **weak reference** means that the
32-
reference count of the object is not incremented and the object will still be
41+
You will need to pay attention to reference counting in case of **weak
42+
references**. In Objective-C, as in Python, creating a weak reference means that
43+
the reference count of the object is not incremented and the object will be
3344
deallocated when no strong references remain. Any weak references to the object
3445
are then set to ``nil``.
3546

36-
Some objects will store references to other objects as a weak reference. Such
37-
properties will be declared in the Apple developer documentation as
47+
Some Objective-C objects store references to other objects as a weak reference.
48+
Such properties will be declared in the Apple developer documentation as
3849
"@property(weak)" or "@property(assign)". This is commonly the case for
3950
delegates. For example, in the code below, the ``NSOutlineView`` only stores a
4051
weak reference to the object which is assigned to its delegate property:

docs/spelling_wordlist

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Alea
2+
alloc
23
Autorelease
4+
autorelease
35
autoreleased
6+
autoreleases
47
Bugfixes
58
callables
69
CPython
@@ -22,6 +25,7 @@ lookups
2225
macOS
2326
metaclass
2427
metaclasses
28+
mutableCopy
2529
namespace
2630
namespaces
2731
ObjC

0 commit comments

Comments
 (0)