Objective C:Object Deallocated while key value obs

2019-02-02 08:28发布

问题:

I am hitting the below error after I added 2 additional fields to my core data model.

CarPark_CarPark_ 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 0x1b6510> (
<NSKeyValueObservance 0x19b210: Observer: 0x1a8cf0, Key path: coordinate, 
Options: <New: NO, Old:     NO, Prior: YES> Context: 0x0, Property: 0x1b7e00>
)

I am a little lost on what to do next. Any guidance on this will be greatly greatly appreciated! Please let me know what other information is required.

回答1:

To set the breakpoint open the breakpoints tab in Xcode 4 (Breakpoints window in Xcode 3) and add a new symbolic Breakpoint for the symbol "NSKVODeallocateBreak"

Use the debugger console to print the observer at the adress given in the observation info

Observer: 0x19af20

po 0x19af20

This should give some valuable information about the observer. Override addObserver:forKeyPath:options:context: in your custom CarPark class and set a breakpoint to see the exact location of the observing being established.



回答2:

It's probably due to the observer being deallocated before the objects being observed. You would need to implement the -(void) dealloc method to remove all observed objects in the observer.