7

I am making an iPhone game. I want to release all objects that have been allocated or retained. In the dealloc function I am releasing all such objects, but then I realized that sometimes I end up releasing objects when they have not been allocated yet. So I figured I need to check if its retainCount is greater than zero or not before I release it.

My question is:

Do I just check if the retainCount is greater than zero and then release it?

if([bg retainCount]!=0)
{
  [bg release];
}

or

Should I release it as many times as its retainCount

while([bg retainCount]!=0)
{
  [bg release];
}

Thanks for your help!

bbum
  • 160,467
  • 23
  • 266
  • 355
abhinav
  • 91
  • 2
  • 5
  • 23
    The `retainCount` of any object is **none of your business**. It is there as a debugging aid, but nothing more. Production code should never ever depend on it. – Sven Sep 16 '10 at 21:11
  • 9
    BTW -- not that it makes the code correct -- but, by definition, `retainCount` can never return 0 because the object has already been deallocated by the time that happens..... – bbum Sep 16 '10 at 21:14
  • Also, try using "Build & Analyze" (i.e. the Clang analyzer). It might point out to you where you a going wrong. – Johan Kool Sep 17 '10 at 00:18

2 Answers2

78

Do not use -retainCount.

The absolute retain count of an object is meaningless.

You should call release exactly same number of times that you caused the object to be retained. No less (unless you like leaks) and, certainly, no more (unless you like crashes).

See the Memory Management Guidelines for full details.

bbum
  • 160,467
  • 23
  • 266
  • 355
  • 10
    You should have that tattooed somewhere. –  Sep 16 '10 at 21:11
  • 4
    @bbum: rdar://8122368 (http://openradar.appspot.com/8122368 for all of us non-apple folks) – Dave DeLong Sep 16 '10 at 21:20
  • I have not used retainCount so far but was wondering if I should. Thanks for clearing that :) I am allocating a foreground image when the player dies in the game. Now if the player completes the level without dying the whole level then in the dealloc how do I check if i should release the foreground or not. Do I just use a flag to check if the foregrounf was used or not? – abhinav Sep 16 '10 at 21:31
  • @abhinav if you only create the image if the player dies, then that means the pointer is `nil` otherwise, right? Then how about checking to see if the pointer is `nil`, or just straight up doing `[myImage release]` (because if `myImage` is `nil`, this is a no op). – Dave DeLong Sep 16 '10 at 21:32
  • If you've never stored anything in an instance variable it'll be nil. Sending release to nil is harmless. So just do [foreground release]; foreground = nil;. – Steven Fisher Sep 16 '10 at 21:33
  • @Dave DeLong & tewha: That's what I was doing, but had some crashes. That's when I started wondering if it was wrong or not. Thanks for clearing that up guys. – abhinav Sep 16 '10 at 21:43
  • If you had crashes, it might likely be due to an over-release. Turn on zombie detection and see if it finds anything. – bbum Sep 16 '10 at 21:45
  • @bbum: I have zombie detection turned on. How does over-release happen? I am giving only one release command in the dealloc function. How can I prevent it? – abhinav Sep 16 '10 at 21:56
  • Over release happens when you release something that you didn't retain in the first place. – bbum Sep 16 '10 at 22:15
  • abhinav: This is why I draw a distinction between over-release (released it too many times) and under-retain (something didn't retain it or make its own copy that should have). As far as the result, it's six of one, half a dozen of the other: You still have something holding onto an object that it doesn't own, and crashing when it tries to use it. But the way you get that result is different, and only you can determine which path your app is taking. Instruments's ObjectAlloc instrument will help you a lot here. – Peter Hosey Sep 16 '10 at 23:00
2

Autorelease makes retainCount meaningless. Keep track of retains & whether you own an object. Study & remember these rules: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH

Mike C.
  • 603
  • 3
  • 8