iPhone Memory Debugging with NSZombie and Instruments
When your iPhone app crashes with ‘BAD ACCESS’ you’re in trouble – a memory bug where you tried to call a method on a object that was already deleted. Instruments has support for NSZombie – a feature that makes it easy to find the source of the bug by showing you a full history of every alloc, retain, release, and autorelease of the object that caused the crash! Wow. Here’s how:
Basic steps are:
- Run in Performance tool ‘Object Allocations’
- Stop running and set options on ObjAllocations instrument: ‘Enable NSZombie’ Detection and ‘Record Reference Counts’
- Re-run from instruments, when it crashes, click the arrow in the popup zombie dialog
- Open up the stack trace view and see the full memory history of the problem object
- Wow, how awesome is that?
- If you love this post, do me a favor and check out our Free app Focus for Facebook
- You might also like my memory management tutorial and other posts under ‘App Development’.
Here’s a link to the demo program: ZombieDebug Demo Project. The code we’re looking at in the video is:
@implementation ZombieDebugViewController @synthesize objArray; -(void)rewriteText { NSMutableString* s = [NSMutableString stringWithCapacity:100]; for (id obj in objArray) { [s appendFormat:@"%@,\n",obj]; [obj release]; } label.text = s; } - (void)viewDidLoad { [super viewDidLoad]; self.objArray = [NSMutableArray arrayWithCapacity:10]; [objArray addObject:@"I'm a string object"]; [self rewriteText]; } -(IBAction) tapButton:(id)button { NSNumber* n = [NSNumber numberWithLong:random()]; [objArray addObject:n]; [self rewriteText]; } -(void)dealloc { [super dealloc]; self.objArray=nil; } @end
Focus for Facebook iPhone App
Announcing our new free iPhone app: Focus for Facebook. Focus presents Facebook® in a streamlined view on your iPhone so you can quickly scan through and stay up-to-date on the go. Focus has some neat features we designed to make it the best mobile Facebook experience we could:
Streamlined Facebook: See new posts, comments, notifications, photos and links in a single, compact, easy-to-scan list. Also you can comment, like, update your status and post photos on the go.
Don’t re-read the same stuff: Focus remembers what you’ve read, and highlights new posts and comments, so you never miss new content.
No more app spam: Focus automatically keeps those games and quizzes out of your feed.
We’ll be updating Focus as soon as we can with Facebook chat, inbox / messages, and full photo browsing. Get Focus for Facebook FREE in the app store now.
Debugging Tip – objc_exception_throw breakpoint
If an exception is thrown when debugging an iPhone app, without your own exception handling code, that exception won’t stop the debugger until the call stack has totally unwound. On that journey through the call stack it gets caught and disguarded in the event loop. That’s a bummer because then the debugger can’t show you where original exception was raised. This problem is easy to fix by adding a symbolic breakpoint for the runtime’s objc_exception_throw function, which is called as soon as the exception occurs. Here’s how:
1) Run the app in the debugger. There’s an exception we can see in the debugger console:
2) Open the debugger from XCode menu Run->Debugger. The stack we can see where the debugger paused is the useless stack where the event loop code caught the exception and then bombed out with its own exception.
3) From XCode menu Run->Manage Breakpoints -> Add symbolic breakpoint we add ‘objc_exception_throw’
4) Now when we debug run again, the debugger stops as soon as the oringinal exception was thrown, we can see exactly where, and we can poke around an inspect variables etc.
Memory Management Basics Tutorial Video
This article is a screen cast video of my tutorial for beginner iPhone programmers, it’s about the basics of memory management in Objective-C. Memory management is a tough nut for the beginner to crack, particularly in Objective-C and Cocoa for iPhone. Check out my iPhone memory management reading list for more voices on memory management.
The tutorial covers: Objective-C object retain counts; using retain, release, and autorelease; explains the autorelease pool in detail and how it works with the event loop; rule of thumb for if an object is in the autorelase pool.
Part 1: retain counts
Part 2: auto-release pool and the event loop
Part 3: auto-release pool and the event loop with retain
Part 4: auto-release pool wrap up
Part 5: properties, dealloc, bugs, auto-release rule of thumb
Thanks for watching!
iPhone Memory Management Reading List
Memory Management in Objective-C for the iPhone gets a lot of beginners confused. It’s a topic that can be explained several different ways, so keep reading and experimenting till it clicks for you. Here’s a list of places to learn about it:
My video lessons: Basics of iPhone Memory Management.
Books
Online
- Practical Memory Management from Apple
- Memory Management Guide from Apple
- Dr Dobbs
- Open Kosmaczewski
- Mac Developer Tips
- Memo.tv
- mauvilasoftware.com
- Stepwise.com
- Tristan O’Tierney
- Mac Developer Network Video
- O’Reilly and here
- Cocoa Dev Central
- Cocoa Dev
- HyperJeff’s list of resources
- WikiBooks
- Devplace
- Woojijuice
- Peter Dikant














