View Full Version : release doesn't change retain count

Mar 3, 2009, 05:31 PM
I am trying to release an object but the retain count doesn't change after the release:

NSLog(@"removeTableViewScreen object retainCount1 [%d]", [myObject retainCount]);
[myObject release];
NSLog(@"removeTableViewScreen object retainCount2 [%d]", [myObject retainCount]);

The output to the console is as follows:

3/3/09 5:23:07 PM removeTableViewScreen myObject retainCount1 [1]
3/3/09 5:23:07 PM removeTableViewScreen myObject retainCount2 [1]

Why wouldn't the release lower the retain count to 0??

myObject is a NSMutableArray with other objects added to it using the addObject method.


Mar 3, 2009, 07:28 PM
I don't think I'd worry about it too much. Per the docco on retainCount:

"This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method."

Duke Leto
Mar 3, 2009, 08:04 PM
Yeah I read the same quote from the docs when I had a crash .. I blamed it on a retainCount of 9 on an object!

Then of course I read the docs... and now the problem is solved!

Mar 4, 2009, 11:03 AM
You're accessing the object after you've released it. I'm suprised the second line prints out at all.

What you might want to do if you really want to know about when your objects are being dealloc'd is overide the release method in each of your classes that you're interested in tracking, and get it to print out [self retainCount] - 1 before calling [super release].

DO MAKE SURE YOU CALL [super release] or things are going to go badly wrong.

Mar 4, 2009, 11:21 AM
DO MAKE SURE YOU CALL [super release] or things are going to go badly wrong.
You mean [super dealloc], right? ;)

Mar 6, 2009, 08:22 AM
You mean [super dealloc], right? ;)

Nope, [super release]. I suggested to the originator of the thread that he override the release method to log the retain count before forwarding it on to the superclass.

Overriding release is something most people will never do.