release doesn't change retain count

Discussion in 'iOS Programming' started by XcodeNewb, Mar 3, 2009.

  1. XcodeNewb macrumors member

    Feb 6, 2009
    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.

  2. tsornin macrumors newbie

    Jul 23, 2002
    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."
  3. Duke Leto macrumors regular

    Mar 17, 2008
    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!
  4. mpatric macrumors newbie

    Oct 20, 2008
    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.
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    You mean [super dealloc], right? ;)
  6. mpatric macrumors newbie

    Oct 20, 2008
    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.

Share This Page