Monday, April 27, 2009

Memory Management tip

Dealing with memory on the iPhone is one of the most difficult
challenges that you will face as a new iPhone developer. Today,
I am going to show you a very simple tip that will help you
debug the memory problems that you are like to face.

One thing that you have to worry about with memory management is
the "retain count" of an object. The retain count is how the
system keeps track of the memory used by an object. If an
object's retain count is zero and your attempt to access it
your app will crash; if you do not make sure the retain
count of your object is 0 when the object goes out of focus you
will have a memory leak.

Clearly, it is important to be able to find out what the current
retain count of an object is. Here is how you can do this:

NSLog([NSString stringWithFormat:@"Retain Count:%i", [someObject retainCount]]);
This writes out the current retain count to the log. The
important function is [someObject retainCount].

No comments: