I was told by a fellow StackOverflow user that I should not use the getter method when releasing a property:
@property(nonatmic, retain) Type* variable;
@synthesize variable;
// wrong
[self.variable release];
// right
[variable release];
He did not explain in detail why. They appear the same to me. My iOS book said the getter on a property will look like this:
- (id)variable {
return variable;
}
So doesn’t this mean [self variable], self.variable, and variable are all the same?
A typical getter will look more like this:
So if you use
[self.variable release]you have an additionalretainandautoreleasethat you don’t really need when you just want to release the object and that cause the object to be released later than necessary (when the autorelease pool is drained).Typically, you would either use
self.variable = nilwhich has the benefit that it also sets the variable tonil(avoiding crashes due to dangling pointers), or[variable release]which is the fastest and may be more appropriate in adeallocmethod if your setter has custom logic.