Take the 2-minute tour ×

How do you remove an observer from an object under ARC? Do we just add the observer and forget about removing it? If we no longer manage memory manually where do we resign from observing?

For example, on a view controller:

[self.view addObserver:self

Previously, I would call removeObserver: in the view controller's dealloc method.

share|improve this question
Note that it's a very bad idea to KVO .frame. As written swhere else by Apple engineers on StackOverflow, UIKit's frame property is not KVO compliant. When it works, it's only by pure chance. –  steipete Dec 4 '11 at 13:04
Shouldn't your keyPath be @"frame" rather than @"self.frame"? –  Besi Aug 10 '12 at 20:29
add comment

3 Answers

up vote 97 down vote accepted

You still can implement -dealloc under ARC, which appears to be the appropriate place to remove the observation of key values. You just don't call [super dealloc] from within this method any more.

If you were overriding -release before, you were doing things the wrong way.

share|improve this answer
Are you sure about this? I quote from clang.llvm.org/docs/…, section 7.1.2. dealloc: "Rationale: even though ARC destroys instance variables automatically, there are still legitimate reasons to write a dealloc method, such as freeing non-retainable resources. Failing to call [super dealloc] in such a method is nearly always a bug. " –  Elise van Looij Nov 28 '11 at 21:42
@ElisevanLooij Yes that's true. If you derive from this class, it seems obvious that you must call [super dealloc]. Who else should do this for you. –  Björn Landmesser Dec 5 '11 at 10:14
@ElisevanLooij Oops, well, should have checked before. It is not allowed to call [super dealloc] in a dealloc method. No idea how this would work then when subclassing the mentioned class. Maybe it is just advisable to use finalize instead (where you call [super finalize]) –  Björn Landmesser Dec 5 '11 at 10:32
@ElisevanLooij - The point they were trying to make there is in regards to the manual memory management case. Because not calling [super dealloc] last in that method is pretty much always a bug under manual memory management, the compiler handles it for you now, which is why you can't call -dealloc directly anymore. The only things you put in a -dealloc method under ARC are any non-object resources you need to free, or cleanup tasks like removing observers. The wording they use is a little muddy, but this is what they meant. –  Brad Larson Dec 5 '11 at 15:13
@BjörnMilcke - As I comment on Elise's answer, -finalize is used for this under garbage collection, where -dealloc is never called, but it's perfectly acceptable to place this code in -dealloc under ARC. [super dealloc] is called for you automatically, which is why it's an error to call it under ARC. –  Brad Larson Dec 5 '11 at 15:21
add comment

I do it with this code

- (void)dealloc
    [self.uAvatarImage removeObserver:self forKeyPath:@"image" context:nil];
} @catch(id anException) {
    //do nothing, obviously it wasn't attached because an exception was thrown
share|improve this answer
What is the point of Exception handling in dealloc? It's too late to do anything about it. –  Abizern Mar 26 at 2:14
add comment

Elsewhere on stack overflow, Chris Hanson advises using the finalize method for this purpose and implementing a separate invalidate method so that owners can tell objects that they are done. In the past I have found Hanson's solutions to be well-thought-out, so I'll be going with that.

share|improve this answer
Note that he was referring to garbage collection there, not ARC (his answer was written in 2008). Under garbage collection, -dealloc is never called. In ARC, it is. It's perfectly acceptable to remove KVO observers in -dealloc, as Chris Lattner (who knows what he's talking about) indicates in Apple's developer forums here: devforums.apple.com/message/475850 –  Brad Larson Dec 5 '11 at 15:18
Thanks Brad, for doing all this work. No to finalize, yes to dealloc but without [super dealloc]. Simple really, once you know it. Hey, @drunknbass, accept that man's answer! –  Elise van Looij Dec 10 '11 at 14:19
add comment

Your Answer


