Wednesday, September 2, 2009

Very simple rules for memory management in iPhone

In iPhone Development You must do your own memory management through reference counting, using -retain, -release and -autorelease.

Retain Count rules

  1. Within a given block, the use of -copy, -alloc and -retain should equal the use of -release and -autorelease.
  2. Objects created using convenience constructors (e.g. NSString's stringWithString) are considered autoreleased.
  3. Implement a -dealloc method to release the instance variables you own.
Method Description
-retain increases the reference count of an object by 1
-release decreases the reference count of an object by 1
-autorelease decreases the reference count of an object by 1 at some stage in the future
-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

No comments:

Post a Comment