Dot dealloc notation?

@property (copy) NSString *name;
@property (copy) NSString *orbit;
@property (copy) NSNumber *mass;
@property float surfaceTemp;
@property float rotationSpeed;

      

Currently there is this

- (void)dealloc{
    [name release];
    name = nil;
    [orbit release];
    orbit = nil;
    [mass release];
    mass = nil;
    [super dealloc];
}

      

If I write this with Dot Notation (Objective-C 2.0) Is it correct?

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

      

Gary

+2


source to share


2 answers


It is bad practice to use setter methods in -dealloc

. Use instead [name release]

.

Calling setters at time -dealloc

can have unintended consequences. If you are using KVO, property settings can trigger other code to run, causing side effects, because your object has already started releasing instance variables. Even if you are not using KVO, it can cause potential problems if your setter method relies on other instance variables that may already be released.



(updated to reflect comments)

+10


source


In my experience self.name = nil

does not do the same as [name release]; name = nil

. I'm guessing there is some code in the synthesized setter that avoids assignments nil

, but YMMV. The properties I observed this were also specified (nonatomic, retain)

so you can see some differences there.



Moreover, the notation self.

sends KVO notifications, so the performance penalty should also be considered in this case.

0


source







All Articles