| By Kevin Hoffman | Article Rating: |
|
| December 15, 2009 09:30 PM EST | Reads: |
1,207 |
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 above:
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.
Read the original blog entry...
Published December 15, 2009 Reads 1,207
Copyright © 2009 Ulitzer, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Kevin Hoffman
Kevin Hoffman, editor-in-chief of SYS-CON's iPhone Developer's Journal, has been programming since he was 10 and has written everything from DOS shareware to n-tier, enterprise web applications in VB, C++, Delphi, and C. Hoffman is coauthor of Professional .NET Framework (Wrox Press) and co-author with Robert Foster of Microsoft SharePoint 2007 Development Unleashed. He authors The .NET Addict's Blog at .NET Developer's Journal.
- Jobs Has a Few Words for Google & Adobe & They Ain’t Pretty: Reports
- My Thoughts on the Apple iPad
- Ulitzer Launches Apple iPad New Media Site
- iPad New Media Topic Launched on Ulitzer
- Developers Are Excited by the iPad and Plan New Apps
- Apple Reports First Quarter Results
- Sybase Mobile University Virtual Online Courses
- iPhone On Your Terms - Allow Choice; Maintain Control
- The Mobile Enterprise UK
- Consolidation of iPhone Applications?
- The iPad As The Door Opener for Bring Your Own Computer " BYOC "
- Apple's iPad & Business Will Make a Knock-out Combination
- Jobs Has a Few Words for Google & Adobe & They Ain’t Pretty: Reports
- My Thoughts on the Apple iPad
- Will Apple Be Transforming Mobile Computing?
- 3-2-1 Go! Apple's Sold-out Worldwide Developers Conference Nears
- Ulitzer Launches Apple iPad New Media Site
- Are We Close to the Saturation Point for iPhone Apps?
- iPad New Media Topic Launched on Ulitzer
- Getting Enterprise Data onto Multiple Platforms
- iPhone Apps "New Media" Topic Launched on Ulitzer
- Geek My Reality Show...Cool!
- Developers Are Excited by the iPad and Plan New Apps
- Security Holes in the Mobile World
- iPhone Developer Summit Extended To June 22-23 By Popular Demand
- iPhone OS 3.0 Hits the Streets Today
- Adobe Flash Media Server on iPhone
- Amazon Releases Kindle for iPhone
- Will the iPhone Soon Be Available from Verizon Wireless?
- Jobs Has a Few Words for Google & Adobe & They Ain’t Pretty: Reports
- iPhone Able to Be Adopted by Enterprise Businesses
- Appcelerator Named "Platinum Sponsor" of AJAX World Conference & Expo
- My Thoughts on the Apple iPad
- My Three iPhone Predictions For 2010
- Seven Steps to the iPhone Developer's World
- What Hardware Changes Will iPhone OS 3.0 Bring?




























Ulitzer content is offered under Creative Commons "Attribution Non-Commercial No Derivatives" License.
For any reuse or distribution, you must make clear to others the license terms of this work.
The best way to do this is with a link to this web page.
Any of the above conditions can be waived if you get written permission from Ulitzer, Inc., the copyright holder.
Nothing in this license impairs or restricts the author's moral rights.