You need to use -removeObserver:forKeyPath:
to remove the observer before -[NSObject dealloc]
runs, so yes, doing it in the -dealloc
method 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 -retain
and -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 -finalize
will run at very different times — and in very different contexts — than -dealloc
did. For one thing, finalization takes place on a different thread, so you really can't safely send -removeObserver:forKeyPath:
to another object in a -finalize
method.
Stick to memory (and other scarce resource) management in -dealloc
and -finalize
, and use a separate -invalidate
method 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…