Debugging Key-Value-Observing overflow.

Posted by Paperflyer on Stack Overflow See other posts from Stack Overflow or by Paperflyer
Published on 2010-04-14T14:29:19Z Indexed on 2010/04/14 14:43 UTC
Read the original article Hit count: 506

I wrote an audio player. Recently I started refactored some of the communication flow to make it fully MVC-compliant. Now it crashes, which in itself is not surprising.

However, it crashes after a few seconds inside the Cocoa key-value-observing routines with a HUGE stack trace of recursive calls to NSKeyValueNotifyObserver. Obviously, it is recursively observing a value and thus overflowing the NSArray that holds pending notifications.

According to the stack trace, the program loops from observeValueForKeyPath to setMyValue and back. Here is the according code:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    if ([keyPath isEqual:@"myValue"] && object == myModel
        && [self myValue] != [myModel myValue]) {
    [self setMyValue:[myModel myValue];
    }
}

and

- (void)setMyValue:(float)value {
    myValue = value;
[myModel setMyValue:value];
}

myModel changes myValue every 0.05 seconds and if I log the calls to these two functions, they get called only every 0.05 seconds just as they should be, so this is working properly.

The stack trace looks like this:

-[MyDocument observeValueForKeyPath:ofObject:change:context:]
NSKeyValueNotifyObserver
NSKeyValueDidChange
-[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:]
-[MyDocument setMyValue:]
_NSSetFloatValueAndNotify
…repeated some ~8k times until crash

Do you have any idea why I could still be spamming the KVO queue?

© Stack Overflow or respective owner

Related posts about key-value-observing

Related posts about overflow