2

I wonder why the retaincount of an object equal one inside its dealloc function .

-(void) dealloc 
{
   NSlog(@"retain count = %i ", [self retaincount]);
   [super dealloc];
}

retain count = 1 so how the object called its dealloc function although its retain count equal 1 . As I know the object calls this function when its retain count is equal zero only .

Popeye
  • 11,839
  • 9
  • 58
  • 91
SamehDos
  • 1,183
  • 1
  • 10
  • 23
  • is this means that the object is still live ????? – SamehDos Oct 27 '11 at 14:32
  • 2
    never count on retainCount http://stackoverflow.com/questions/4636146/when-to-use-retaincount – vikingosegundo Oct 27 '11 at 14:35
  • 1
    I agree, don't use retain count as all of your other answers posted. But what happens if you call `[super dealloc]` first – James Webster Oct 27 '11 at 14:42
  • if I call [super dealloc]; first , the object will be removed from memory so I will not be able to use self . But what will make me crazy is hoooooooow the object called its dealloc although its retain count equal 1 . – SamehDos Oct 27 '11 at 14:54
  • But as Mr. vikingosegundo and Mr.jbat100 told me not to use retaincount to debug . – SamehDos Oct 27 '11 at 14:55
  • See my answer; retainCount can never be zero because there would be no point. – bbum Oct 27 '11 at 15:00

2 Answers2

9

Because the retain count of an object can never be zero.

Decrementing it to zero is a waste of cycles because the object will b deallocated anyway. retainCount can never return 0.

And:

retainCount is useless. Do not call it.

Thank you .But how I can debug the existence of an object ??

Several ways;

  • NSLog() in the -dealloc
  • breakpoint on dealloc, print something, continue
  • use Zombies
  • use Allocations instrument
  • use Heapshot analysis
bbum
  • 162,346
  • 23
  • 271
  • 359
3

You can consider the object to be still alive in the dealloc method before the [super dealloc]; call. This allows you to call messages on self to do some cleaning up within the dealloc. This is why [super dealloc]; should always be called last. This post goes into some detail about why you shouldn't give any importance to the retain count in dealloc (or for that matter, anywhere).

Community
  • 1
  • 1
jbat100
  • 16,757
  • 4
  • 45
  • 70