dPompa Blog

Memory Warnings (aka: EXC_BAD_ACCESS)

As an iPhone developer you have to handle low memory situations gracefully and clear as much memory as you can. These can often lead to unexpected crashes if your memory management has a bug somewhere. If you wish to catch these easily then do the following:

  1. Go to your executable settings in Xcode and set the environment variable NSZombieEnabled to YES.

  2. Go to System Preferences > Keyboard > Keyboard Shortcuts and add a shortcut for the iPhone Simulator menu item called “Simulate Memory Warning”. I set mine to command+1.

    Simulate Memory Warning Shortcut

  3. Add a breakpoint (cmd + option + B in Xcode) at “objc_exception_throw”.

Now run your app in the simulator under the debugger, and trigger memory warnings while working your app. Trigger often, at the places your app probably won’t expect them. In fact, try triggering as much as you can.

The shortcut you defined allows you to send the warnings while the mouse operates the app. Since NSZombie throws an exception on each message it receives, you’ll break exactly where you need.

Have fun and good luck! :)

Leave a Reply