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.

Ben Snider

Benjamin Snider

Hi! 👋 I'm Ben and I like to write about technical and nerdy things. Historically about Swift and iOS. But, I've recently started a masters program in computer science (Georgia Tech's OMSCS), so the content here may pivot as such.  Get @me!