Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot notation dealloc?

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

Currently Have this

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

If I write this using Dot Notation (Objective-C 2.0) Is this right?

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

gary

like image 390
fuzzygoat Avatar asked Aug 07 '26 20:08

fuzzygoat


1 Answers

It's bad practice to use your setter methods in -dealloc. Use [name release] instead.

Calling setters during -dealloc may have unintended consequences. If using KVO, setting properties may trigger other code to run causing side effects because your object has already started releasing instance variables. Even when not using KVO this may cause potential problems if your setter method relies on other instance variables that may have already been released.

(updated to reflect comments)

like image 95
pix0r Avatar answered Aug 10 '26 13:08

pix0r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!