2
2
Memory management for Objective-C instances
3
3
===========================================
4
4
5
+ Reference counting in Objective-C
6
+ =================================
7
+
5
8
Reference counting works differently in Objective-C compared to Python. Python
6
9
will automatically track where variables are referenced and free memory when
7
10
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
13
16
memory management will be inserted for you at compile-time. However, since
14
17
Rubicon Objective-C operates at runtime, it cannot make use of ARC.
15
18
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.
18
33
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.
25
37
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
+ ------------------------------
29
40
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
33
44
deallocated when no strong references remain. Any weak references to the object
34
45
are then set to ``nil ``.
35
46
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
38
49
"@property(weak)" or "@property(assign)". This is commonly the case for
39
50
delegates. For example, in the code below, the ``NSOutlineView `` only stores a
40
51
weak reference to the object which is assigned to its delegate property:
0 commit comments