by hiraki9 · 15y ago · 0 comments
- (void) dealloc
{
self.caption = nil;
self.photographer = nil;
[super dealloc];
}
Using the dot syntax will invoke a setter whose implementation might change if the class is subclassed, which can be the cause of subtle errors. Also, if you're using KVO (Key-Value Observing), invoking the setter might notify observers who then try to work with a (partially) deallocated object.This is standard:
- (void) dealloc
{
[caption release];
[photographer release];
[super dealloc];
}My problem with this argument is that the user could botch release just as well. Maybe they are slightly more likely to botch the setter, but either way they are botching it.
Since your object is likely to be dealloced at an odd time (for instance when an autorelease pool is drained), and in an inconsistent state (half its fields missing), it's much better to do a release.
- (void)dealloc {
self.someOtherVariable = nil;
self.myC = nil; // Sends KVO on myC. B sees half-deconstructed A.
self.myB = nil; // myB now is dealloc'd and stops observing A.
[super dealloc];
}
Admittedly this is a real edge case and probably bad design, but it's not overtly terrible. I have a couple cases where objects observe their owners.BTW thanks for the blog, Mike! I love it.