From 03e7d81ffa79887d2f45adefdadd427e9d7ec7f6 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Tue, 26 Apr 2011 11:16:36 +0200 Subject: [PATCH] fail more gracefully when attempting to remove the same observer twice Before this commit we would fail gracefully for single keys. For key paths an application would crash because we would try to get the forwarder property of an undefined object. This commit makes sure we don't attempt any removal and logs a warning (just like Cocoa) about the attempt to remove an unregistered observer. --- Foundation/CPKeyValueObserving.j | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Foundation/CPKeyValueObserving.j b/Foundation/CPKeyValueObserving.j index fbac24c09..ee72d91fc 100644 --- a/Foundation/CPKeyValueObserving.j +++ b/Foundation/CPKeyValueObserving.j @@ -678,6 +678,14 @@ var kvoNewAndOld = CPKeyValueObservingOptionNew | CPKeyValueObservingOpti { var observers = _observersForKey[aPath]; + if (!observers) + { + CPLog.warn(@"Cannot remove an observer %@ for the key path \"%@\" from %@ because it is not registered as an observer.", + _targetObject, aPath, anObserver); + + return; + } + if (aPath.indexOf('.') != CPNotFound) { var forwarder = [observers objectForKey:[anObserver UID]].forwarder;