Professional Documents
Culture Documents
Memory Management InObjective C
Memory Management InObjective C
Memory-Management Rules
Memory-management rules, sometimes referred to as the ownership
policy, help you to explicitly manage memory in Objective-C code.
• You own any object you create by allocating memory for it or
copying it.
• If you are not the creator of an object, but want to ensure it stays
in memory for you to use, you can express an ownership interest
in it using method retain.
• If you own an object, either by creating it or expressing an
ownership interest, you are responsible for releasing it when you
no longer need it using release and auto release method.
• Conversely, if you are not the creator of an object and have not
expressed an ownership interest, you must not release it.
Aspects of Memory Management
Unless there is some other reference to the parent or child, they both become orphaned. But the retain cycle between the
parent and child prevent either from being released and they become wasted memory.
A child should never retain a parent. If anything, use a weak reference in the child to maintain a reference to the parent.
A retain cycle is a situation when object A retains object B, and object B retains object A at the same time*. Here is an
example:
@class Child;