Debugging EXC_BAD_ACCESS

suggest change

EXC_BAD_ACCESS means the process tried to access memory in an invalid way, like dereferencing a NULL pointer or writing to read-only memory. This is the hardest kind of crash to debug, because it usually does not have an error message, and some crashes can be very difficult to reproduce and/or occur in code completely unrelated to the problem. This error is very rare in Swift, but if it occurs, you can often get easier-to-debug crashes by reducing compiler optimizations.

Most EXC_BAD_ACCESS errors are caused by trying to dereference a NULL pointer. If this is the case, the address listed in the red arrow will usually be a hexadecimal number that is lower than a normal memory address, often 0x0. Set breakpoints in the debugger or add occasional printf/NSLog statements to find out why that pointer is NULL.

An EXC_BAD_ACCESS that occurs less reliably or makes no sense at all could be the result of a memory management problem. Common problems that can cause this are:

In Edit Scheme, under Run → Diagnostics, Xcode includes tools that help debug memory problems:

Address Sanitizer stops the app when it detects an invalid memory access and reports what happened. Zombie Objects detect messages sent to deallocated Objective-C objects. Automatic Reference Counting does not prevent those messages, so zombies are still useful in an ARC app.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents