1

Here is retaincount code.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        NSNumber *number = [[NSNumber alloc]initWithInt:10];
        NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:0];

        NSLog(@"retain count : %d",[number retainCount]);

        [array addObject:number];
        NSLog(@"retain count : %d",[number retainCount]);

        [number release];
        NSLog(@"retain count : %d",[number retainCount]);

        [number release];
        NSLog(@"retain count : %d",[number retainCount]);

        [number release];
        NSLog(@"retain count : %d",[number retainCount]);
    }
    return 0;
}

My expected answer is

retain count : 1
retain count : 2
retain count : 1
retain count : 0
and then error

but actually running result is as following.

[Switching to process 6363 thread 0x0]
2011-12-01 19:39:53.843 nsnumber[6363:707] retain count : -1
2011-12-01 19:39:53.846 nsnumber[6363:707] retain count : -1
2011-12-01 19:39:53.847 nsnumber[6363:707] retain count : -1
2011-12-01 19:39:53.847 nsnumber[6363:707] retain count : -1
2011-12-01 19:39:53.848 nsnumber[6363:707] retain count : -1

I can't understand this result.

Why is this the result will come?

dasdom
  • 13,539
  • 2
  • 41
  • 53
S.J. Lim
  • 2,803
  • 2
  • 33
  • 52

4 Answers4

3

Don't trust the value of retain count. Especially as you are using ARC! Follow the rules. That's all.

dasdom
  • 13,539
  • 2
  • 41
  • 53
  • +1 The implication is that you can't use retainCount with ARC. http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html – JeremyP Dec 01 '11 at 12:06
  • AFAIK, this wouldn't even compile under ARC; using `retainCount` throws an error. – jscs Dec 01 '11 at 21:40
3

NSNumber returns singleton instances for certain numbers (I believe integers 1 to 12, or something).

You shouldn't ever rely on the value of retainCount. Any number of things could be happening throughout your code or framework code that can change the retain count.

Jasarien
  • 57,071
  • 27
  • 154
  • 186
1

About 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.

See also

http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/retainCount

When to use -retainCount?

Community
  • 1
  • 1
beryllium
  • 29,214
  • 15
  • 99
  • 123
1

The return type of retainCount is NSUInteger; that's an unsigned integer. The string format specifier %d is for a signed integer.

Cocoa uses NSUIntegerMax to represent the retain counts of immortal objects -- objects which will never be released. For performance reasons, it caches and reuses NSNumber objects representing small integers -- this NSNumber that you've created is apparently one of those, and is immortal.

When you interpret the maximum unsigned integer value as if it were signed (under two's complement arithmetic), it appears to be -1.

To see the "true" value, you should use the specifier %lu, as indicated by the chart I linked to above.

However, you generally shouldn't rely on retainCount to give you any useful information. This is documented:

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.

as beryllium already noted.

Community
  • 1
  • 1
jscs
  • 62,161
  • 12
  • 145
  • 186