How to debug EXC_BAD_ACCESS bug

Adam Lee picture Adam Lee · Nov 2, 2013 · Viewed 27.3k times · Source

I received an error

EXC_BAD_ACCESS code=2 at0xb0987654

I am wondering how to print out the value at 0xb0987654?

Answer

Jasper Blues picture Jasper Blues · Nov 2, 2013

To debug an EXC_BAD_ACCESS, you can generally find out the where the dangling pointer is by enabling zombie objects.

Xcode

Choose edit scheme, then Diagnostics tab in the Run section, then click the 'Zombie Objects' option.

AppCode

Choose edit target, and add the following environment variable:

NSZombieEnabled=YES

Another cause for EXC_BAD_ACCESS can be infinite recursion, which can be found by adding some logging.

Update for C++:

To debug dangling pointers in C++ with the Clang compiler try using Address Sanitizer (ASAN) from Google.