views:

100

answers:

2

Hey,

I kind of understand what's retain counts for. But not totally. I looked on google a lot to try to understand but still I don't.

And now I'm in a bit of code (I'm doing iPhone development) that I think I should use them but don't know totally how.

Could someone give me a quick and good example of how and why using them?

Thanks!

+4  A: 

If you haven't already I highly recommend you familiarize yourself with the Memory Management Programing Guide from Apple

In there you will find a section specifically on retain counts

ctshryock
+3  A: 

The best explanation I ever heard was from Aaron Hillegass:

Think of the object as a dog. You need a leash for a dog to keep it from running away and disappearing, right?

Now, think of a retain as a leash. Every time you call retain, you add a leash to the dog's collar. You are saying, "I want this dog to stick around." Your hold on the leash insures that the dog will stay until you are done with it.

Think of a release as removing one leash from the dog's collar. When all the leashes are removed, the dog can run away. There's no guarantee that the dog will be around any longer.

Now, say you call retain and put a leash on the dog. I need the dog, too, so I walk along with you and start training him. When you are done with the dog, you call release and remove your leash. There are no more leashes and the dog runs away, even though I was still training him!

If, instead, I call retain on the dog before I start training him, I have a second leash on the collar. When you call release and remove your leash, I still have one and the dog can't go away just yet.

Different objects can "own" the dog by calling retain and putting another leash on its collar. Each object is making sure that the dog doesn't go away until it is done with it. The dog can't go away until all of the leashes have been removed.

retainCount is like counting the number of leashes on the dog's collar.

Autorelease pools get more complicated, but simplistically you can think of calling autorelease as handing your leash to a trainer. You don't need the dog anymore, but you haven't removed your leash right away. The trainer will take the leash off later; there is still no guarantee that the dog will be around when you need him.

dmkash
That's a nice way of visualising it. I would also note that `new`, `alloc` and `copy` all provide you with a dog on your leash. And you need to take the leash off!
ohhorob
+1 that's great. Only the autorelease part needs work. Here's my effort: You want to leave the park, but Aarron wants to play with the dog. You can hand them the leash, but park rules say you can only do that if your name is Al Lock, begins with New like Mr Newman or contains Copy like Bill Smicopyth's. The alternatives are to take your leash off and then Aaron puts his on. Unfortunately, there is a short period of time during which the dog can run away between those. Or leave the leash on, let Aaron attach his and rely on him to detach yours.
JeremyP
Aaron is absent minded and would probably forget to remove your leash. Even worse, he doesn't know if you left your leash on hoping he would remove it, or took it off hoping the dog wouldn't run away before he had time to put his on. The answer is that you give your end of the leash to a nearby vagrant (whose name is Otto R Leespule). Otto won't run away with the dogs because he is also on a leash held by Aaron. When Aaron is done playing with the dog, he just lets go of Otto's leash.
JeremyP
@ohhorob Thanks for adding that! Getting an instance of an object using anything other than `new`, `alloc`, or `copy` gives you an autoreleased object -- a dog without a leash.Oh, and a dog with a leash left on is called a memory leak. :D
dmkash