The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 4,878,821
since: 19 Jan 2005

Debugging EXC_BAD_ACCESS Errors with NSZombieEnabled

posted Tue 15 Dec 09

One of the biggest problems developers have when building apps for the iPhone is memory violations. A lot of developers, especially those who come from a Java or .NET background, have trouble adjusting to an environment where they need to manually keep track of reference counts and they need to be explicitly aware of their own memory management.

I've known quite a few developers who tried their hand at programming for the iPhone and they threw up their hands and quit when they started trying to track down memory problems. I'm certainly not condoning that kind of behavior, I'm just recognizing that the lack of built-in garbage collection represents both a learning curve and a frustration obstacle for new iPhone developers, whether they've been doing other programming for 2 or 20 years.

Thankfully, there's something you can do about it. One of the biggest problems people get is an EXC_BAD_ACCESS error that terminates their debug session, but the callstack is empty and it's nearly impossible to figure out which method call caused the problem. There are several causes for EXC_BAD_ACCESS but the #1 most common cause for this is the attempt to send a message to a released object. The problem arises from the fact that since the released object isn't there, the bad access occurs and the call stack goes away before the debugger stops.

This is where NSZombie comes in. NSZombie is quite possibly one of the coolest, most clever tricks I've ever seen in a programming environment. The way it works is, when you have zombies enabled (will show you how to do this in a second), a released object becomes a zombie when released. What this really means is that instead of deallocating and moving on, the original object is deallocated and an instance of NSZombie is left in its place. The sole purpose of NSZombie is to yell at you whenever someone tries to send it a message. Any message sent to a zombie will halt the debugger and put an entry in your console log telling you that you just tried to send a message to a released object.

I cannot even begin to describe how useful this is. This preserves the call stack so that you can see the exact line of code that sent a message to the released object. While this won't necessarily tell you what you did to prematurely release the object (premature release... too funny), it will tell you which object was released and what message you were trying to send and it'll preserve the call stack to give you other valuable debugging information.

To enable NSZombies all you need to do is double-click the application file in your "Executables" group and go to the parameters tab. At the bottom you'll see an area to configure environment variables. Add an environment variable called NSZombieEnabled and set its value to YES. Your screen should look like the screenshot below: 

 

Now that zombies are enabled, you will be able to run your application and know that when you send a message to a released object, you're going to get a nice friendly execution termination, complete with call stack. You'll know which message was sent and you'll know which objects have been released because when you hover over them after the break, you'll see which ones are concrete instances (still alive) and which ones have been released (they'll be an instance of NSZombie).

Moral of the story: Not only are the undead good for entertainment value and destroying the earth in an unforgiving plague of destruction, but they're handy debugging tools as well.

tags:        

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Alex Blewitt left...
Tue 15 Dec 09 2:52 pm :: http://alblue.blogspot.com

Thanks for the great tip. There's a lot of good stuff you can do with environment variables - for example, setting OBJC_HELP=1 prior to running dumps out a bunch of messages that you can debug.

There's also NSObjCMessageLoggingEnabled=YES, which dumps out the callpath of messages as the code executes. That can be pretty handy :-)


2. Aaron Burghardt left...
Wed 16 Dec 09 5:55 am

This and many other great tips are documented in the technote "Mac OS X Debugging Magic":

http://developer.apple.com/mac/library/technotes/tn2004/tn2124.html


3. Kevin Hoffman left...
Thu 17 Dec 09 2:26 pm

Thanks for the link. Great info!


Tag Related Posts

Would you like to touch my mono?

Mon 23 Nov 09 2:59 P GMT-05

Apple drops the iPhone NDA for Released Software

Wed 01 Oct 08 3:54 P GMT-05
tags:          

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

iPhone Developer Summit

Sun 16 Mar 08 8:39 P GMT-05

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

Celebrity Death Match: iPhone vs. gPhone

Tue 06 Nov 07 1:52 A GMT-05
tags:    

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Editorial : My thoughts on iPhone Hacking

Tue 25 Sep 07 6:10 P GMT-05
tags:      

$4800 iPhone Bill for International Data Roaming

Mon 10 Sep 07 1:15 P GMT-05
tags:    

iPhone Apps are now also iPod Touch Apps

Sat 08 Sep 07 7:33 P GMT-05
tags:    

Using the iPhone Keyboard

Fri 31 Aug 07 2:08 P GMT-05
tags:      

Meebo on my iPhone - a useable IM client!

Tue 24 Jul 07 2:56 P GMT-05
tags:    

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Wil Shipley Discusses the lack of iPhone SDK

Mon 09 Jul 07 1:50 P GMT-05
tags:    

OMFGWTF iPhonez0rz!!1!one!!

Fri 29 Jun 07 5:45 P GMT-05
tags: