How many times do I release an allocated or retained object?
Asked Answered
S

2

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!

Scrooge answered 16/9, 2010 at 21:3 Comment(3)
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.Ileum
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.....Eldin
Also, try using "Build & Analyze" (i.e. the Clang analyzer). It might point out to you where you a going wrong.Apophasis
E
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.

Eldin answered 16/9, 2010 at 21:7 Comment(10)
You should have that tattooed somewhere.Acklin
@bbum: rdar://8122368 (openradar.appspot.com/8122368 for all of us non-apple folks)Clavius
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?Scrooge
@Scrooge 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).Clavius
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;.Joyce
@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.Scrooge
If you had crashes, it might likely be due to an over-release. Turn on zombie detection and see if it finds anything.Eldin
@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?Scrooge
Over release happens when you release something that you didn't retain in the first place.Eldin
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.Pretension
S
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

Seaton answered 16/9, 2010 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.