Take the 2-minute tour ×

I'm creating an iPad game that has a viewController that loads in its view from a nib file. The view has a bunch of buttons in it, which I linked up to UIButton * variables via interface builder (so each button is linked to a different variable). I checked the retain count right after they nib was loaded on one of the buttons (using my first button variable, b1) and it gives me a value of 2. Can anyone explain why it's 2? What are the two things that are retaining it right after the nib loads?

And now I'm even more confused because in my dealloc function, I released each of the button variables individually, and checked the retain count for one of them after and it's STILL 2! It should at least have gone down to 1, shouldn't it have? Should I release it several times in my dealloc function? If so, how many?


share|improve this question
Show some code. –  jamapag Jul 6 '11 at 15:54
Are you linking them to retained properties? –  shawnwall Jul 6 '11 at 15:55
Try profiling using instruments. The memory leak checker should show you where it's being retained. –  Maz Jul 6 '11 at 15:55
@shawnwall I'm not linking them to retained properties. I figured it would make no difference since I never explicitly set the button variables (thus a setter is not needed) @Maz thanks I'll try that –  Tim Jul 6 '11 at 16:30
add comment

2 Answers

Don't look at retain count.


Things other than you retain your stuff. Those numbers will move around underneath you for reasons that appear to make no sense, and then you'll come back here and post bewildered questions.

Just make sure your retains and releases balance. That's your only job.

share|improve this answer
On an iOS device it is even more important, due to the limited memory, that all memory is being used efficiently. If there is a failure, the end user doesn't care if it was you or Apple that failed, they'll just blame you. –  Maz Jul 6 '11 at 15:58
Ok, thanks. One follow-up question: does it matter if I have my IBOulets set as properties if I never explicitly set or get them? (As in the IBOutlet button variables I connected up to the buttons in the nib file)? And should I release all those button variables at the end, even though I never explicitly retained them (they were set up via interface builder)? –  Tim Jul 6 '11 at 16:13
@Tim - When your view loads, they'll be retained (assuming you used the 'retain' setter semantic in your property declarations). So you need to release them. –  Dan Ray Jul 6 '11 at 16:46
@Dan Ray - I didn't actually make the button IBOutlets properties, I just declared them as variables in my header. Does this mean I don't have to worry about releasing them? (I never explicitly set any of them either). –  Tim Jul 6 '11 at 17:30
@Tim - No, it means you haven't retained them, and that's dangerous. Definitely want to make them properties, using the 'retain' setter semantic, and explicitly release them in -dealloc. –  Dan Ray Jul 6 '11 at 17:32
show 2 more comments

If your IBOutlet properties are retained then you would have 1 retain there and another when the button gets added to the super view...

As Dan Ray says though, you shouldn't really worry about the retainCount...

share|improve this answer
add comment

Your Answer


