-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from SamSchott/memory-management
Autorelease objects which we own
- Loading branch information
Showing
5 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Autorelease Objective-C instances when the corresponding Python instance is destroyed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ stand alone. | |
|
||
get-started | ||
type-mapping | ||
memory-management | ||
protocols | ||
async | ||
c-functions | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
=========================================== | ||
Memory management for Objective-C instances | ||
=========================================== | ||
|
||
Reference counting works differently in Objective-C compared to Python. Python | ||
will automatically track where variables are referenced and free memory when | ||
the reference count drops to zero whereas Objective-C uses explicit reference | ||
counting to manage memory. The methods ``retain``, ``release`` and | ||
``autorelease`` are used to increase and decrease the reference counts as | ||
described in the `Apple developer documentation | ||
<https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html>`__. | ||
When enabling automatic reference counting (ARC), the appropriate calls for | ||
memory management will be inserted for you at compile-time. However, since | ||
Rubicon Objective-C operates at runtime, it cannot make use of ARC. | ||
|
||
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. | ||
|
||
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. | ||
|
||
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 | ||
reference count of the object is not incremented and the object will still be | ||
deallocated when no strong references remain. Any weak references to the object | ||
are then set to ``nil``. | ||
|
||
Some objects will store references to other objects as a weak reference. Such | ||
properties will be declared in the Apple developer documentation as | ||
"@property(weak)" or "@property(assign)". This is commonly the case for | ||
delegates. For example, in the code below, the ``NSOutlineView`` only stores a | ||
weak reference to the object which is assigned to its delegate property: | ||
|
||
.. code-block:: python | ||
from rubicon.objc import NSObject, ObjCClass | ||
from rubicon.objc.runtime import load_library | ||
app_kit = load_library("AppKit") | ||
NSOutlineView = ObjCClass("NSOutlineView") | ||
outline_view = NSOutlineView.alloc().init() | ||
delegate = NSObject.alloc().init() | ||
outline_view.delegate = delegate | ||
You will need to keep a reference to the Python variable ``delegate`` so that | ||
the corresponding Objective-C instance does not get deallocated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters