The only time you should release self

October 18th, 2011

The only time you should call [self release] is if during the initialization of your object an error occurs that causes you to return nil.

- (id) initWithData:(NSData *)userData error:(NSError **)error
{

      if(!userData){
           
           if(error){
                  *error = //set up error      
           }          

          [self release];
        
          return nil;
      }

//more code

}

This is because if we just return nil, we will have alloc'd memory but never be able to release it so we need to release it while we have the chance.

What do you think? Let me know by getting in touch on Twitter - @wibosco

William Boles

William has been an iOS developer since 2010, after all these years he just hopes that the String API will stop changing now.