0

Possible Duplicate:
When to use -retainCount?

I was trying to understand autorelease pools. I created a sample application as below:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSString *releasePoolString = [[[NSString alloc] initWithFormat:@"%@",@"ReleasePool autorelease variable"] autorelease];

NSLog(@"Retain count of autorelease variable inside release pool %i",[releasePoolString retainCount]);

[pool drain];

// After pool drain still retain count = 1 ??????
NSLog(@"Retain count of autorelease variable after release pool drain %i",[releasePoolString retainCount]);

The last log still prints retaincount as 1. Am I missing something ... can someone please help me understand ....

Thanks...

Community
  • 1
  • 1
Ben861305
  • 101
  • 8

2 Answers2

4

retainCount never reaches 0 because when it's 1 and release is called, it calls immediately dealloc without decrement retainCount

Johnmph
  • 3,391
  • 24
  • 32
0

Because the retainCount wont give exact retain count of an object always. You should not depend on retainCount API for memory management. Once you alloc, retain an object, you have to release that object and its retain count will become zero.

rakeshNS
  • 4,227
  • 4
  • 28
  • 42