Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

message sent to deallocated instance error

Im constantly being given an error that reads *** -[NSKeyValueObservance retain]: message sent to deallocated instance 0x86c75f10. I have tried running the Zombies template and here is the screenshot of what it provides.

enter image description here

It points to a managedObject, and I'm having trouble figuring out where the object is being deallocated. Here is the block of code that the compiler takes me to after each crash.

- (void)setIsFavourite:(BOOL)isFavourite shouldPostToAnalytics:(BOOL)shouldPostToAnalytics;
{
    // check whether we need to generate preferences objects just in time
    if(!self.preferences && !self.series.preferences /*&& isFavourite*/)
    {
        if(self.series)
        {
            [self.series addPreferencesObject];
        }
        else
        {
            [self addPreferencesObject];
        }
    }

    //Crash In here
    self.preferences.isFavourite = @(isFavourite);
    self.series.preferences.isFavourite = @(isFavourite);

EDIT: If you need to see a larger size of the image here is a larger resolution link.

like image 537
TheM00s3 Avatar asked Feb 10 '15 23:02

TheM00s3


1 Answers

OK, I hit something similar and found a way to debug this kind of issue with NSKeyValueObservance. To debug, do the following:

  1. In Xcode, open the "Breakpoint Navigator".
  2. Add a new symbolic breakpoint with: -[NSKeyValueObservance _initWithObserver:property:options:context:originalObservable:]
  3. To that breakpoint, add an action and set it to "Debugger Command".
  4. Set the following command: expr (void)NSLog(@"observer <0x%p>: %@ <%p>, property: %@", $arg1, (id)NSStringFromClass((id)[(id)$arg3 class]), $arg3, (id)$arg4)
  5. Click the "Automatically continue after evaluating expression".

Now you can run your application and take the steps necessary to reproduce your crash. And yes, you'll want NSZombies enabled. Note: it's going to run slow and you're going to get a ton of debug output, but just be patient. It'll get there eventually.

When you hit the crash when trying to message a deallocated NSKeyValueObservance, you'll be presented with the address of the original object. Highlight the address and hit cmd-e to enter the text in the search buffer. Then hit cmd-g find the next occurrence of the string in the debugger output. You're going to potentially find the address a couple of times, so look for the address that follows the observer <0x?????> output. The output on that line should tell you what object is being observed and for which property.

In my case, when I figured this all out, it turned out that I was observing a synthesized property that depended on an object in array and during a certain operation, the order of the objects in the array changed without doing the correct KVO notifications, and that caused my crash.

like image 77
A.J. Raftis Avatar answered Oct 21 '22 07:10

A.J. Raftis