The standard way to create an object in Objective-C looks like this:
MyClass* object = [[MyClass alloc] init];
The standard implementation of MyClass‘s init method would look something like this:
-(id) init
{
self = [super init];
if(self) { /* initialize */ }
return self;
}
Aside from some syntax changes, and excluding factory methods, that seems to be the recommended way to write an init method, and to use it.
As I understand it, the purpose of self = [super init]; is to handle the case where [super init] fails. But if it does fail, and returns nil, wouldn’t there be a memory leak? The reason being that MyClass‘s init will return nil, object will be nil, there will be no more pointers that reference the object allocated with [MyClass alloc], and therefore no way to release it.
These are the two solutions I can think of are, but I haven’t seen either one in regular practice.
After a call to alloc, check the results before calling init:
MyClass* object = [MyClass alloc];
if(object == nil) { /*handle the error */ }
else { object = [object init]; }
Or, if [super init] fails, release the memory. Something like this:
-(id) init
{
id temp = [super init];
if(!temp) { [self release]; }
self = temp;
if(self) { /* initialize */ }
return self;
}
Am I wrong in that reasoning? It could be argued that [super init] is unlikely to fail, but then why assign the results of it to self and check for nil? I’d be happy to see some clarification.
If
[super init]wants to return nil, it should also callreleaseon self.