When I’ve registered an object foo to receive KVO notifications from another object bar (using addObserver:…), if I then deallocate foo do I need to send a removeObserver:forKeyPath: message to bar in -dealloc?
When I’ve registered an object foo to receive KVO notifications from another object bar
Share
You need to use
-removeObserver:forKeyPath:to remove the observer before-[NSObject dealloc]runs, so yes, doing it in the-deallocmethod of your class would work.Better than that though would be to have a deterministic point where whatever owns the object that’s doing the observing could tell it it’s done and will (eventually) be deallocated. That way, you can stop observing immediately when the thing doing the observing is no longer needed, regardless of when it’s actually deallocated.
This is important to keep in mind because the lifetime of objects in Cocoa isn’t as deterministic as some people seem to think it is. The various Mac OS X frameworks themselves will send your objects
-retainand-autorelease, extending their lifetime beyond what you might otherwise think it would be.Furthermore, when you make the transition to Objective-C garbage collection, you’ll find that
-finalizewill run at very different times — and in very different contexts — than-deallocdid. For one thing, finalization takes place on a different thread, so you really can’t safely send-removeObserver:forKeyPath:to another object in a-finalizemethod.Stick to memory (and other scarce resource) management in
-deallocand-finalize, and use a separate-invalidatemethod to have an owner tell an object you’re done with it at a deterministic point; do things like removing KVO observations there. The intent of your code will be clearer and you will have fewer subtle bugs to take care of.