0

I'm having a model object, a direct NSObject subclass, whose properties are observed by a context object using KVO.

I'm unregistering from KVO in the dealloc of the model object like this:

- (void) dealloc
{
    [self.context unregisterObject:self];
}

The context's method looks like this:

- (void) unregisterObject:(MyCustomObject*) inObject
{   
    for (NSString *property in [inObject propertyNamesToObserve])
    {
        [inObject removeObserver:self forKeyPath:property context:(void*)kCustomContext];
    }
}

Still I'm getting a message from the runtime as posted below, so I'm wondering if -dealloc is too late to unregister from KVO?

An instance 0x10045fc10 of class MyCustomObject 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:

According to this post, what I'm doing should be alright: KVO and ARC how to removeObserver

Or am I overlooking something? I checked that the context is non-nil when dealloc is called.

Community
  • 1
  • 1
iljawascoding
  • 1,090
  • 9
  • 20

1 Answers1

2

Figured it out. The model object was registered for KVO by the context twice, but unregistered only once.

iljawascoding
  • 1,090
  • 9
  • 20