Break on EXC_BAD_ACCESS in Xcode?

后端 未结 10 1979
北恋
北恋 2020-11-27 03:14

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

相关标签:
10条回答
  • 2020-11-27 03:21

    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];
    
    0 讨论(0)
  • 2020-11-27 03:23

    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.

    0 讨论(0)
  • 2020-11-27 03:25

    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.

    0 讨论(0)
  • 2020-11-27 03:25

    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/

    0 讨论(0)
  • 2020-11-27 03:27

    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

    0 讨论(0)
  • 2020-11-27 03:36

    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.

    0 讨论(0)
提交回复
热议问题