views:

165

answers:

1

When my application is closed, the main controller class removes itself as Observer from the model and then releases the model. Like this:

- (void)dealloc {
    [theModel removeObserver:self
                  forKeyPath:@"myValue"];
    [theModel release];
    [super dealloc];
}

And right after that, the debugger says:

2010-04-29 14:07:40.294 MyProgram[13678:a0f] An instance 0x116f2e880 of class TheModel was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x100288450> (
<NSKeyValueObservance 0x1002aca90: Observer: 0x116f40ec0, Key path: myValue, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x116f80430>
)

where 0x116f2e880 is indeed the model and 0x116f40ec0 is indeed the controller.

How can the controller still be an observer when it just removed itself as an observer?

+2  A: 

This is just a guess, but did you add the controller as an observer for that key path more than once? Perhaps with different options or context? Or did you add it as an observer to a different object but with a key path that resolves to the model object? e.g. if foo has a property theModel and you added the controller as an observer to foo with the key path "theModel.myValue".

Could you put a breakpoint on -addObserver:forKeyPath:options:context: and see what gets added and when?

JeremyP
Do I have to explicitly remove observers that got implicitly added with `+keyPathsForValuesAffecting<key>`?
BastiBechtold