I\'m new to iPhone development and Xcode in general and have no idea how to begin troubleshooting an EXC_BAD_ACCESS
signal. How can I get Xcode to break at the
Just wanted to add for the others who are coming from a web, searching for solutions for the same error but with different mistake. In my case I got the same error when I tried to instantiate NSDictionary with typo in key name where I forgot to add "@" in front of my key:
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: myObj1, @"goodKey", myObj2, "badkey @ is missing in front", nil];
From the Stanford CS193P classes: if you add a breakpoint (manually, by editing breakpoints) for the symbolobjc_exception_throw
you can get a much better picture of what went wrong - letting things proceed to the point where the debugger halts by itself tends to obscure things and screw up the stack trace. When you halt in objc_exception_throw you can often look back to exactly what access/operation caused your problem.
Xcode/gdb always breaks on EXC_BAD_ACCESS
, you just need to work your way up the call stack to find the code that triggered it.
Note that these kinds of errors often occur with autoreleased
objects, meaning that the ultimate cause of the problem won't be in the call stack that triggered EXC_BAD_ACCESS
. That's when NSZombieEnabled and NSAutoreleaseFreedObjectCheckEnabled become helpful.
Another helpful approach is set breakpoints that will trigger directly after the exception occurs:
Open the breakpoints window (Run – Show – Breakpoints) and add two symbolic breakpoints called “objc_exception_throw” and “[NSException raise]"
From: http://blog.emmerinc.be/index.php/2009/03/19/break-on-exception-in-xcode/
In Xcode 4, you can enable Zombies by clicking on the Scheme dropdown (top left, right next to the stop button) -> Edit Scheme -> Diagnostics Tab -> Enable Zombie Objects
About your array. The line
NSMutableArray *array = [NSMutableArray array];
does not actually give you a retained object but rather an autorelease object. It probably gets retained in the next line but then you should not release it in the third line. See this
This is the fundamental rule:
You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.