NSZombie to the Freaking Rescue

So I’ve been beating my head against a wall the past few days trying to cram as much Objective-C knowledge as possible and I think it’s all starting to click.

Today I stumbled over the NSZombieEnabled argument and HOLY CRAP WHY ISN’T THIS DEFAULT BEHAVIOR. After enabling the argument I found where my app crashed in about 1 minute. Turns out you should not do:

- (void)viewDidUnload {
    self.foo = nil;
    [super viewDidUnload];
}

but rather

- (void)viewDidUnload {
    foo = nil;
    [super viewDidUnload];
}


A fairly subtle difference I’ll grant myself but, apparently, an important one. In this particular case I am using the self.foo property as a typed copy of the UIViewController’s view property. In the wrong case setting self.foo to nil releases the property where the framework assumes it still exists, resulting in an app crash. Simply setting the foo instance variable to null, however, works great.

This entry was posted on Wednesday, May 5th, 2010 at 11:21 am and is filed under Objective-C, iPad, iPhone. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply