Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zombie Objects From Views instantiated from Interface Builder

I've got a crash that happens at a critical point in our application. However, the crash appears to be coming from an overreleased UIView object (more specifically the header view of a UITableView, which in this case is a UISearch bar). The odd thing about this is that that was instantiated from a nib file. Here's the crash...

I've setup my nib file to match exactly what Apple has in one of its sample projects.

enter image description here

I've also taken the time to analyze this with NSZombieEnabled and using Instruments. However I still can't make heads or tails as to why this is happening. Here's the Pairing of Retain/Releases....

enter image description here

And the stack trace exposing when the UIView object is created.

enter image description here

The common example of this type of error is an NSError object that is created and assigned to an out parameter within an autorelease block. However, I would not expect to see the same problem with a UIView. Nor do I see an autorelease block anywhere in my code (save the one on the main run loop). Any thoughts on how I might solve this problem?

like image 752
Ryan Pfeffer Avatar asked Oct 22 '13 16:10

Ryan Pfeffer


1 Answers

The Problem is, that listening for notifications or adding an KVO observer does not change the retaincount of the observing object. And there seems to be a notification fired right after the deallocation of you're view/viewcontroller/object.

You could changed the - (void)dealloc Method to remove the notification/KVO observer from your view/viewcontroller

Notifications:

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //if you are not using ARC you also have to write
    //[super dealloc];
}

For KVO use Method with your keypath(s)

- (void)removeObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath

Cheers, Nils

like image 94
Nils Ziehn Avatar answered Oct 03 '22 17:10

Nils Ziehn