On Sep 30, 2012, at 11:38 AM, Kyle Sluder wrote: > On Sun, Sep 30, 2012, at 10:57 AM, Lee Ann Rucker wrote: >> Weird that you didn't get the warning, but we've switched to the pattern >> of >> >> [self addObserver:self forKeyPath:@"ctrl.selectedObjects.someString"... >> >> and that's saved countless headaches. You can remove your own observers >> in dealloc so you never need to worry about when to remove the observer. > > Self-observation has always been a code smell to me. I suppose it's a > lot better now that we have -removeObserver:forKeyPath:context:. > > In general, it is not a good idea to rely on -dealloc for cleanup of > anything other than object references (that is, sending -release to > strong ivars). Any other resource that requires explicit management > (NSNotificationCenter registration, KVO, file handles, etc.) should not > be managed in -dealloc unless that resource is *explicitly* tied to the > lifetime of the object (a reuse pool, logging). >
> I'd add and remove my observation in -setCtrl:. Or, since this is a view > controller, perhaps add an explicit teardown method > (-removeViewFromWindow or -windowIsClosing or something) and remove my > observation there. > We've tried that. Either way you have to do cleanup somewhere; dealloc isn't the best place, but not all objects have nice convenient teardown hooks like windows and views do. Doing this in a setter means custom setters all over the place, so no nice @synthesize, and fun when a subclass needs to add a different set of observation values. Plus you'll have to call a setter in dealloc or duplicate the removeObserver code, neither of which are optimal. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com