Search code examples
iphoneobjective-ciosretaindealloc

Why does my property getter cause an NSZombie error?


I have a service object called JSONNetworkUtility, and I store it in my model as an ivar, as well as a synthesized property with the same name, nonatomic and retained:

myNetworkUtility = [[JSONNetworkUtility alloc] initNetworkConnectionWithURL:urlString withQueryString:nil delegate:self];

The delegate includes two callbacks, one is networkUtility:didFailWithError: and the other is networkUtility:didFinishWithData:. The weird thing is that the property is causing some weird errors:

- (void)networkUtility:(JSONNetworkUtility *)networkUtility didFinishWithData:(NSArray *)jsonArray
{
    NSLog(@"myNetworkUtility = %@", myNetworkUtility);
    // returns <JSONNetworkUtility: 0x3944450>

    NSLog(@"networkUtility = %@", networkUtility);
    // also returns <JSONNetworkUtility: 0x3944450>

    NSLog(@"self.myNetworkUtility = %@", self.myNetworkUtility);
    // fails and throws an NSZombie error!
}

The error I get on that line is:

*** -[MyModel myNetworkUtility]: message sent to deallocated instance 0x148190

I'm totally stumped! Any clues as to why it's failing on a getter? And why is it returning a completely different object?

The reason I'm using a getter is because I wanted to use self.myNetworkUtility = nil so I can write over the top of the property with a new object, but I've cut it back to just this trace and I'm still having problems...

Thanks!


Solution

  • I think the problem is that MyModel is deallocated. When you're accessing the ivar, the objects are still there in that memory address address, the memory is not yet corrupted. But when sending a message to deallocated 'self' the runtime catches the fact that the object is deallocated.