July 22nd, 2010 Add Your Comments

In my line of work I get to talk to a lot of iPhone developers who are new to Objective-C. And no matter how much experience said developer has in other languages, one problem they always face in the beginning is understanding and debugging Objective-C’s memory management scheme. But don’t fret young Skywalker! Everything you need to know about Objective-C memory management can be learned from a street busker (aka performer).

Let’s set up some simple rules first:

1) Picture every Objective-C object as a street busker with a hat lying on the ground in front of him.

2) If you want that street busker to stay around after playing his first song (or in our case, completes a method block) then you need to throw a dollar into that hat.

3) If there are no dollars in that hat after the song is over, then the busker gets fed up and leaves and you’ll never see him again.

4) As long as there are one or more dollars in that hat, the street busker will keep happily playing his music.

5) When that street corner closes for the evening (i.e. when your parent object is ready to be released– usually in the dealloc method), you want your street busker to leave that corner for good.

6) If your busker stays around after closing, then you’ll never get rid of him (i.e. a memory leak).

With me so far?

Now the beauty of this street busker is that he is quite “worldly” and can accept dollars in different currencies. To him they are all equal. These currencies are:

1) alloc (example: [MyCoolObject alloc])

2) retain (example: [myobject retain])

3) property assignment (example: self.myobject = anotherObject). Note: this is only if the property is defined as @property(nonatomic, retain), and basically this is the same thing as [myobject retain].

Now you can also choose a couple of ways to take that money from him– the nice way or the not so nice way.

The Nice Way: autorelease (example: [myobject autorelease]) – The busker will be nice and stay around for a bit until the song is over. But if the dollar you take this way is the last one, he’s going to pack up and leave like a nice guy.

The Not So Nice Way: release (example: [myobject release]) – The busker doesn’t like the way you taunt him as you pull our your dollar, and if the dollar you take is the last one, he leaves immediately.

Okay so now that we understand this busker business, how do we apply this in the real world to quickly debug memory issues in Objective-C? Just follow these steps:

1) Take an object and do a search for all the places your object appears in your code file.

2) If you see an alloc, retain, or property assignment (i.e. self.myobject = x) next to that object, add 1 dollar to that object.

3) If you see a release or autorelease next to that object (self.myobject = nil is actually the same as [myobject release]), remove 1 dollar for that object.

4) Once you’ve gone through your entire file and have added/subtracted dollars to determine the final amount in the hat, you can use the following rules to determine the outcome:

$0 = Everything is perfect– the busker plays as long as you want him to and leaves when the street closes.

> $0 = Memory leak– you forgot to take that last dollar away and hey just keeps on playing forever, and ever.

< $0 = App will crash with an EXC_BAD_ACCESS error– the busker realizes that not only have you taken the last dollar out of his hat, but you’ve just pilfered a buck from his wallet! He punches you in the face and the police show up and ruin everyone’s day.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Reddit Post to StumbleUpon