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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to