Re: Properly writing an Init method
Re: Properly writing an Init method
- Subject: Re: Properly writing an Init method
- From: Ali Ozer <email@hidden>
- Date: Wed, 17 Jul 2002 17:31:27 -0700
- (id) init
{
if(self = [super init]) {
//initialize some stuff
//...
if(/* test something here*/)
return NULL; <- Memory leak?
}
return self;
}
If the initialization is not successful, I return NULL, however the
memory
has already been allocated when I called [[MyClass alloc] init], so
should I
add: [self dealloc] or [self release] before returning NULL in the
init
method, or even [super dealloc]?
You want to do a [self release] before returning nil.
Actually, the init method which first fails should do the release; in
this case, [super init] returned nil, so it should have released...
You release only if you get success from the super, but then decide
you can't init for some reason...
Oh; seems like I missed the second if-test; yes, [self release] is
appropriate above, in the "if(/* test something here */)" case...
Ali
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.