views:

588

answers:

2

Hi,

I am making an iPhone game. I want to release all the object that have been allocated or retained. In the dealloc function I am releasing all such objects but then realized 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 the release is

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

or

Should I release it as many number of times as its retainCount

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

Thanks for your help

Abhinav Chandran

+32  A: 

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
You should have that tattooed somewhere.
Graham Lee
@bbum: rdar://8122368 (http://openradar.appspot.com/8122368 for all of us non-apple folks)
Dave DeLong
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
@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
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
abhinav
If you had crashes, it might likely be due to an over-release. Turn on zombie detection and see if it finds anything.
bbum
@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
Over release happens when you release something that you didn't retain in the first place.
bbum
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
+1  A: 

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.