Trace EXC_BAD_ACCESS exception in iPhone app with Xcode 4

Question: Recently I upgraded my universal iPhone project to Xcode 4 and ran immediately into an EXC_BAD_ACCESS exception in my application when running on an iPad. My code runs without errors on iPhone. I don’t know where it has come from and how to find the source code line in error. My app quits without saying anything and the debugger jumps to the main.m module. Console messages do not help either.

Answer: EXC_BAD_ACCESS exceptions usually happen when your code wants to use objects that have already been released. When the error is caught, the call stack is usually has gone on vacation. This is the probable reason of the silent exit and the jump to the main.m as the last resort of your debugger.

There is a well known NSZombieEnabled environment variable that, when it’s enabled, leaves a dummy object behind for every deallocated object. It lets us trace released objects while chasing an EXC_BAD_ACCESS exception. When NSZombieEnabled is set to YES, the class of all deallocated objects is dynamically changed to _NSZombie. The memory region will never free, so NSZombieEnabled should not be left in place otherwise you will spend hours trying to find memory leaks. Do not forget to set its value to NO when you are ready.

Where and how to set NSZombieEnabled to YES in Xcode 4?

1. Go to your project in Xcode 4 2. Environment variables can be set up in the Scheme editor (find Product -> Edit scheme… in menu bar):
Debugging EXC_BAD_ACCESS exception with Xcode 4
Set NSZombieEnabled to YES in Xcode 4
3. Name the variable as NSZombieEnabled and set its value to YES. 4. Run your app in Debug mode. You likely get a … message sent to deallocated instance… message and the debugger stops at the line in error. This information leads to the problem. 5. When ready, disable NSZombieEnabled by changing YES to NO. PS: I checked your code. You should apply retain property in your subclasses:
@interface myNavigationBar : UINavigationBar <UINavigationBarDelegate> {
    UIImage *imageBackground;
@property (nonatomic, retain) UIImage *imageBackground;

@implementation myNavigationBar
@synthesize imageBackground;