Should I always release self for failed init methods?

Posted by leo on Stack Overflow See other posts from Stack Overflow or by leo
Published on 2010-03-18T05:22:33Z Indexed on 2010/03/18 5:31 UTC
Read the original article Hit count: 219

Filed under:
|
|
|

Should I always release self when there is a failure inside init, or should I only do so if I have initialized instance variables first?

To put it another way, is this pattern valid? Is there a time when I shouldn't release self inside an init method, or should I assume that if the control flow enters init, self has at least a retain count of 1?

- (id)init
{
 if ((self = [super init]) == nil)
 {
  [self release];
  return nil;
 }

 //do some init stuff
 if (somethingFailed)
 {
  [self release];
  return nil;
 }
 return self;
}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa