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 likely 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].
That is it - know how to find out the retain count of a particular object will give you some help in the fight against randomly crashing (or leaky) apps.