discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why does an NSNumber start off with a retain count of 2?


From: Issac Trotts
Subject: Why does an NSNumber start off with a retain count of 2?
Date: Wed, 22 Mar 2006 12:48:10 -0800

Here's a test program that shows the odd behavior.

address@hidden /tmp $ cat numcrash.m
#import <Foundation/Foundation.h>
#include <assert.h>

int main(int argc, char *argv[])
{
   NSAutoreleasePool* pool = [NSAutoreleasePool new];
   NSNumber* n = [[NSNumber alloc] initWithInt:5];
   NSString* s = [[NSString alloc] init];
   NSMutableArray* ma = [[NSMutableArray alloc] init];
   NSArray* a = [[NSArray alloc] init];
   NSLog([NSString stringWithFormat:@"n's retain count is %i.\n",
           [n retainCount]]);
   NSLog([NSString stringWithFormat:@"s's count is %i.\n",
           [s retainCount]]);
   NSLog([NSString stringWithFormat:@"ma's retain count is %i.\n",
           [ma retainCount]]);
   NSLog([NSString stringWithFormat:@"a's retain count is %i.\n",
           [a retainCount]]);
   assert([n retainCount]==1);
   [pool release];
   return 0;
}

address@hidden /tmp $ gcc -o numcrash numcrash.m -lobjc -I
/usr/GNUstep/System/Library/Headers/
-fconstant-string-class=NSConstantString -lgnustep-base
-L/usr/GNUstep/System/Library/Libraries -Wno-import

address@hidden /tmp $ ./numcrash
2006-03-22 20:46:37.000 numcrash[5510] Unknown time zone name `PST'.
2006-03-22 20:46:37.000 numcrash[5510] Using time zone with absolute offset 0.
2006-03-22 20:46:37.000 numcrash[5510] n's retain count is 2.
2006-03-22 20:46:37.000 numcrash[5510] s's count is 1.
2006-03-22 20:46:37.000 numcrash[5510] ma's retain count is 1.
2006-03-22 20:46:37.000 numcrash[5510] a's retain count is 1.
numcrash: numcrash.m:19: main: Assertion `[n retainCount]==1' failed.
Aborted




reply via email to

[Prev in Thread] Current Thread [Next in Thread]