Re: Unexpected behaviour with autorelease pool
Re: Unexpected behaviour with autorelease pool
- Subject: Re: Unexpected behaviour with autorelease pool
- From: Filip van der Meeren <email@hidden>
- Date: Sun, 14 Dec 2008 22:36:28 +0100
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...
Filip van der Meeren
email@hidden
http://www.sourceforge.net/projects/perlmanager
http://www.sourceforge.net/projects/xlinterpreter
On 14 Dec 2008, at 18:55, Krishna Kotecha wrote:
Hi,
I am seeing some unexpected behaviour and was hoping someone might
be able
to shed some light on this.
I have a basic Counter class. The relevant methods for the problem
are:
-init
{
if (self = [super init]) {
//counter = [[NSNumber alloc] init];
counter = [NSNumber numberWithInt:0]; // lets grab an autorelease
object
}
return self;
}
-(void) dealloc
{
[counter release];
[super dealloc];
}
I also have some client code (a Foundation Tool project):
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Next two lines will cause a runtime error. I understand this, as
I am
releasing
// an object I dont own...
//
// NSNumber* myNumber = [NSNumber numberWithInt:22];
// [myNumber release];
// Question: Why don't the next 2 lines cause a runtime error?
// In the Counter:dealloc method, I am also releasing an object I dont
own...
Counter* myCounter = [[Counter alloc] init];
[myCounter release];
[pool drain];
return 0;
}
As the comment say, I would expect an error to occur due the fact
Counter:dealloc: is releasing an autorelease object. However the
application
exits cleanly.
Shouldn't this code be causing an error at some point? And if not,
why not?
Any insights or explanations any one has on this would be appreciated.
Thanks.
Regards,
Krishna
_______________________________________________
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
_______________________________________________
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