Re: Properly writing an Init method
Re: Properly writing an Init method
- Subject: Re: Properly writing an Init method
- From: Ondra Cada <email@hidden>
- Date: Wed, 17 Jul 2002 22:35:00 +0200
On Wednesday, July 17, 2002, at 07:04 , Pierre-Olivier Latour wrote:
Say I have an init method like this one:
- (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]?
[self release] (or perhaps, to stay on the safe side, [self autorelease])
looks like the best bet, unless your -dealloc happens to be written so
that it fails unless init was finished (which would be a bug anyway).
Besides, you should return nil, not NULL. For the compiler there's no
difference, but for a human reader there is.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
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.