tags:

views:

33

answers:

1

And is it a common idiom in Objective-C.

I've only seen this used on [[NSImage alloc] initWithContentsOfFile: str] and it always make me think there is a memory leak, because i called alloc and the mantra is: "Call alloc and call you must call release" - unless its one of the cases where you don't need too.

+3  A: 

It is a common idiom to indicate a error in initializing the object. You are correct, however, the allocated instance must be released. So the pattern would be

- (id)init
{
  self = [super init];
  if(self != nil) {
    //... do init
    if(errorInInit) {
      [self release];
      return nil;
    }
  }

  return self;
}
Barry Wark