Alex Rivera | Logout

In Cocoa do I need to remove an Object from receiving KVO notifications when deallocating it?

Asked 2008-08-17T21:05:13.083
23

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?

Edit
Report

1 Answer

2

Definitely agree with Chris on the "Stick to memory (and other scarce resource) management in -dealloc and -finalize..." comment. A lot of times I'll see people try to invalidate NSTimer objects in their dealloc functions. The problem is, NSTimer retains it's targets. So, if the target of that NSTimer is self, dealloc will never get called resulting in some potentially nasty memory leaks.

Invalidate in -invalidate and do other memory cleanup in your dealloc and finalize.

answered 2008-09-15T18:16:34.263

Your Answer