Search code examples
iosobjective-cdealloc

How to use dealloc when dealing with auto-synthesized properties?


I'm relatively new to iOS development so please excuse me if this is a retarded question. I've read this but am still a bit confused.

I'm not using ARC. (Yes yes, I know I should but I don't at this point) In my class header I have this

/*-----------------------------------------------------------------------+
 | The name of the sender/receiver
 +-----------------------------------------------------------------------*/
@property (nonatomic, retain) NSString *name;

I do NOT synthesize this variable but let the compiler do that job.

What of the following is considered to be best practise for the dealloc method

#1 Dealloc the iVar

-(void) dealloc {
   [_name release];
   [super dealloc];
}

#2 Dealloc the property

-(void) dealloc {
   [self.name release];
   [super dealloc];
}

#3 And a last question. Is is customary to set the property to nil in the dealloc method? I.e.

-(void) dealloc {
   [self.name release];
   self.name = nil;
   [super dealloc];
}

Would really appreciate if someone could explain this to me.

Regards!


Solution

  • Jeff Lamarche has written a nice article about releasing variables in the dealloc: http://iphonedevelopment.blogspot.nl/2010/09/dealloc.html

    He suggest never to use the self. syntax, since it can cause problems in a multi threaded environment.

    His suggestion is to use the iVar and set in to nil in production builds:

    -(void) dealloc {
       [_name release], _name = nil;
       [super dealloc];
    }