Retain, release. A guide to Cocoa Memory Management

Intro to Cocoa memory management

For those coming to the iPhone (and Obj-C/Cocoa) platform, especially those coming from any platform with garbage collection will definitely get caught out by Cocoa’s memory management model. This will probably hit those developing using the iPhone SDK hard, with its limited memory catching leaks will be ever more important as when memory runs low your app will have to clean up after itself and exit :( .

For those reading Cocoa code for the first few times calls to methods such as -retain-release and -autorelease might seem somewhat bizarre, but they form the basis of Cocoa memory management. Cocoa uses reference counting to keep track of memory and the code that uses it, a common practice in low memory devices. These calls are all you need to make sure you aren’t causing memory leaks in your code.

Method Description
-retain increases the reference count of an object (by 1)
-release decreases the reference count of an object
-autorelease decreases the reference count of an object by 1 when possible
-alloc allocates memory for an object, and returns it with retain count of 1
-copy makes a copy of an object, and returns it with retain count of 1

Basic Rules

Good things come in three, so here are the three golden rules for memory management for iPhone development

  1. Within a block of code, the use of -copy-alloc and -retain should be balanced by use of -release and -autorelease.
  2. Objects created using convenience constructors (e.g. NSString’s stringWithString) are autoreleased.
  3. Implement a -dealloc method to release the instance variables you own when your class is destroyed

Examples

Maintaining the reference counts for an object

- (void)helloWorld{
NSString *string;
//alloc the memory for sting object
string = [[NSString alloc] initWithString:@"Hello World"];
// we created string with alloc so we need to release it so the
//referance count for this code block match
[string release];
}

Creating an object using a convienience constructor, autrelease is used.

- (void)helloWorld
{
NSString *string;
// object is created with a convenience constructor, will be autoreleased
string = [NSString stringWithFormat:@"Hello"];
// autorelease, no need for release
}

Working with arrays, and objects stored in arrays. Objects placed inside an array in can be released after addition since the array object will add 1 to the reference count


NSMutableArray *array;
int i;
for (i = 0; i < 10; i++)
{
NSNumber *n = [[NSNumber alloc] initWithInt: i];
[array addObject: n];
[n release];
}

Since its quite a difficuly area for people new to Cocoa/iPhone I’ll add more memory management tips as and when, and I’ll certainlly attempt to answer any questions left in the comments.

  • Reddit
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • NewsVine
  • TwitThis
  • LinkedIn
  • Technorati
  • Netvibes
  • StumbleUpon
  • MySpace
  • SphereIt

Similar Posts

Leave a Reply