EXC_BAD_ACCESS when doing KVO(Key-Value Observing)

2016-03-02 03:36:05 -0500

If you are getting EXC_BAD_ACCESS when a KVO event is generated, you probably forget to remove the observer from listening a object when the observer is deallocated

All you need to do is to call removeObserver:forKeyPath in observer's dealloc:

- (void)dealloc {
    @try {

        [SomeObj removeObserver:self forKeyPath:@"price"];

    } @catch (NSException * __unused exception) {}
}

If you do not remove the observer, the KVO system will be trying to access the memory space which is not available anymore for the reference KVO is using

(lldb) bt
* thread #1: tid = 0xb77a18, 0x000000010e66e80b libobjc.A.dylib`objc_msgSend + 11, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x000000010e66e80b libobjc.A.dylib`objc_msgSend + 11
    frame #1: 0x000000010bf5fce7 Foundation`-[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:] + 380
    frame #2: 0x000000010bfbe35f Foundation`_NSSetObjectValueAndNotify + 261
«Newer      Older»
Comment:
Name:

Back to home

Subscribe | Register | Login | N