Wednesday, December 8, 2010

GDB: Program received signal: "EXC_BAD_ACCESS"

When your program crashes (not if, but when) with an error message...
GDB: Program received signal: "EXC_BAD_ACCESS" know that there is most likely a memory leak somewhere. Your code just tried to access something, which was supposed to be there, which used to be there, which is going to be there, which should have been there - but right now is missing.

Open debugger (Command-Shift-Y) and check the traceback. If you're lucky, there might be a line or few from your code. Most likely you'll just see system calls without a clue which part of your app might be related to the crash. You're facing hours of more or less random debugging, adding NSLog() calls and breakpoints trying to isolate the location of actual crash.

Don't worry, there is an easier way: Guard Malloc! Special version of memory allocation library, fine-tuned to help debugging memory issues. Sounds scary, but it's so easy that you have no excuses to avoid it:
  • It's build-in, no need to install anything
  • Activate via menus (on - off)
  • Just use your application, no changes there
  • Check results from debugger, no changes there
Time needed to learn to use it: well, since you've read this far, you already know all you need to know. Activate two menu items (Run - Stop on Debugger()/DebugStr() as well as Run - Enable Guard Malloc), run your program trying to crash, open Debugger and check traceback again. Now it should pinpoint you to the offending code, which caused the memory leak.

Please note that you might want to disable Guard Malloc, when you're not debugging leaks. It's a bit slower than normal iOS simulator, even though if you have any background with mobile java or Symbian development, you might not notice too much a difference. Qt developers and iPhone developers will notice the speed bumb right away.

No comments:

Post a Comment