Re: Unexpected behaviour with autorelease pool
Re: Unexpected behaviour with autorelease pool
- Subject: Re: Unexpected behaviour with autorelease pool
- From: Ken Thomases <email@hidden>
- Date: Sun, 14 Dec 2008 15:47:41 -0600
On Dec 14, 2008, at 3:36 PM, Filip van der Meeren wrote:
I think I have found the answer to your question; when executing the
following code, I get a few strange results...
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSNumber *n0 = [NSNumber numberWithInt:1];
NSLog(@"n0: %d", [n0 retainCount]);
[n0 release];
NSLog(@"n0: %d", [n0 retainCount]);
[n0 release];
NSNumber *n1 = [NSNumber alloc];
NSLog(@"n1: %d", [n1 retainCount]);
n1 = [n1 initWithInt:1];
NSLog(@"n1: %d", [n1 retainCount]);
[n1 release];
[pool release];
The code above results in the following log:
2008-12-14 22:32:54.997 SmallTest[556:10b] n0: 2 <===============
Thats strange....
2008-12-14 22:32:55.003 SmallTest[556:10b] n0: 1
2008-12-14 22:32:55.004 SmallTest[556:10b] n1: -1 <===============
That is normal
2008-12-14 22:32:55.005 SmallTest[556:10b] n1: 2 <===============
Whooooow, overretained an object ;-)
My guess is that NSNumber is over-retaining itself within its
initializer. Nothing for you to worry about.
This is a typical worry-case for that nice Apple programmer... You
should file a bug-report...
This is why examining retain counts will only confuse you.
It is perfectly reasonable for NSNumber to vend cached, shared copies
of commonly used values. For example, 0 or 1.
Stop trying to figure out what the retain count "should be" at any
given point, nor having expectations about when exactly an object will
be fully released and hence deallocated.
Just pay attention to your own responsibilities.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden