Memory Management Tips

This page provides some tips about memory management in Objective-C. It isn't a substitute for a thorough understanding. For that, please refer to Apple's document:

Introduction to Memory Management Programming Guide for Cocoa

This page can be found at:

iphone.timefold.com/training/resources/memorymgt.html



Memory Management Tips

About retain, release and dealloc
From Introduction to Memory Management Programming Guide for Cocoa:
When you create an object, it has a retain count of 1. When you send an object a retain message, its retain count is increased by 1. When you send an object a release message, its retain count is decreased by 1 (autorelease causes the retain count to be decremented in the future).

When its retain count drops to 0, an object's memory is reclaimed In Cocoa terminology it is "freed" or "deallocated". When an object is deallocated, its dealloc method is invoked automatically. The role of the dealloc method is to to free the object's own memory, and dispose of any resources it holds, including its object instance variables.

If your class has object instance variables, you must implement a dealloc method that releases them, and then invokes super's implementation. For example, if the Thingamajig class had name and sprockets instance variables, you would implement its dealloc method as follows:

- (void)dealloc {
    [sprockets release];
    [name release];
    [super dealloc];
}

Parents Retain Children, but not vice versa
If a parent object has a pointer to a child object, that pointer should be retained. If the child object has a pointer to its parent, that pointer should be assigned. References to parent objects are called "weak references."

You should never invoke another object's dealloc method directly
The dealloc method will be called automatically when the object's retain count falls to zero and it falls out of scope.

You do not need to release objects created by convenience methods unless you have retained them.
Objects created with class methods (+) or convenience methods that allocate and initialize an object and return to you will autorelease the object. If you set one of your properties to it then that property should retain and ultimately release it. But, if you just use the object and don't retain it, do not release it, or it will have one too many releases!

If you use alloc or a method that begins with new or init or if you use a method that contains the word copy, you will have to release or autorelease the object returned by the method.

Always use self when setting property values
In this example:
self.topImage=[UIImage imageWithContentsOfFile:topIconPath];
The topImage property will end up pointing to the UIImage object returned by the UIImage class's imageWithContentsOfFile class method. Assuming we declared this property with retain like this:
@property (nonatomic, retain) UIImage *topImage;
Then a release will be called for any previous object pointed to by topImage and a retain will be called for the newly assigned value.

When you add an object to a collection, such as an array or dictionary, that collection will retain it
If you have a collection property, you should retain that, but you don't need to separately retain each of the elements you put into the set. When you release the collection, it will release all its members when it is deallocated. Of course, if an object in a collection needs to be retained because it has a separate, independent use, you certainly can retain it and release it on your own; however, if its only use is to be a member of a collection, it should not be separately retained and released.


Further reading: