Finding The Source of Autorelease EXC_BAD_ACCESS Errors Using gdb & XCode

Posted by Tres Sun, 24 May 2009 01:56:00 GMT

So you’re seeing EXCBADACCESS messages in gdb and your stack trace includes an attempted autorelease that traces back to main without a hopping into a method or class otherwise. This, my friends, is what happens when we try releasing memory that’s already been released.

Finding the source of an attempted bad memory release is a difficult task unless you build your app with the following environment variables:

NSDebugEnabled
MallocStackLogging
NSZombieEnabled
MallocStackLoggingNoCompact

All it takes is setting all of these to YES in XCode (double-click the app in the Executables group of your project Groups and Files, then go to the Arguments tab. Variables are set in the bottom portion of the dialog.)

Once you’ve got the environment variables in place, build normally & then when the app crashes, find the PID of the app and the memory address attempting to be accessed. Then use mallochistory to trace things back to their source. You can do this right from gdb like this: shell mallochistory [PID] [memory_address].

Example:


[Session started at 2009-05-23 18:29:16 -0700.]
2009-05-23 18:29:16.951 MensaMaster[65026:20b] *** -[CFString release]: message sent to deallocated instance 0xee30f0
GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:43:13 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".Undefined command: "NSZombieEnabled".  Try "help".
Undefined command: "NSZombieEnabled".  Try "help".
Setting environment variable "NSDebugEnabled" to null value.
NSZombieEnabled
MallocStackLogging YES
Attaching to process 65026.
(gdb) shell malloc_history 65026 0xee30f0

I’ve highlighted the gdb output in red that needs to be used in malloc_history.

Now you can also use Instruments to accomplish the same task, but I leave that for another post.

Time to get back to work.

Posted in ,  | Tags ,