This procedure has saved me more than once, so I need to document it throughly for everyone! There are cases where I see messages (in the debugger console) that are like this;
-[NSFetchedResultsController class]: message sent to deallocated instance 0x11957d0
Sometimes, it will talk about “freed” instead of deallocated. That address at the end is important. To track down the line of code where the object was allocated, you’ll need to set a couple environment settings. To do this, right click on the executable you’re trying to debug and select “get info”. Select the “arguments” tag and set the values shown below;

debug environment settings
(be sure to disable these when you’re done. They should not be enabled for a production build!)
Next, run your program and test enough to generate the error. In the Debugger Console, copy that hex address value, then type the command;
(gdb) info malloc-history <paste-address-here>
You should see something like this;
Stack – pthread: 0xa0416720 number of frames: 28
0: 0x9264382d in malloc_zone_calloc
1: 0x92643782 in calloc
2: 0x93611618 in _internal_class_createInstanceFromZone
3: 0x9361ab08 in _internal_class_createInstance
4: 0x3020275c in +[NSObject allocWithZone:]
5: 0x3020264a in +[NSObject alloc]
6: 0x3d92 in -[AMRAPViewController fetchedResultsController] at /Users/dkavanagh/CrossFit Timer/Classes/AMRAPViewController.m:146
7: 0x3a18 in -[AMRAPViewController viewWillAppear:] at /Users/dkavanagh/CrossFit Timer/Classes/AMRAPViewController.m:96
8: 0x3097c945 in -[UINavigationController _startTransition:fromViewController:toViewController:]
9: 0x30977c33 in -[UINavigationController _startDeferredTransitionIfNeeded]
10: 0x3097d01e in -[UINavigationController pushViewController:transition:forceImmediate:]
The first line below the object alloc (in my case, line 6) will show where the offending object was allocated and should really help in tracking down the free/dealloc problem.
The original solution was posted in the app dev forums. You’ll need a login to see this.
Hey David,
Trying to get a hold of you regarding your typica library. We’re doing all sorts of fun iphone development over here at #mellmo.
Do you mind emailing me with your contact info? I have some questions for you :-).
Thx.
Paul Walker
http://www.twitter.com/pjwal
paul@paulwalker.tv
m.310.922.2629
It should be MallocStackLoggingNoCompact that is set to 1, not MallocStackLoggingNoComp as seen in the screenshot. The “Name” column needs to be just a little wider.
And yes, if you leave these variables on, you will be scratching your head wondering why you have such a big memory leak. 🙂
Thank you thank you thank you.
Thank you! You just saved my life — I had been looking in all the wrong places until I found this debugging aid.
Hi ! I am confused when I run
info malloc-history 9430 0x50dedd0
A syntax error near end of expression.
From the gdb prompt I get an error report? It doesn’t understand this command.
Normally I run “shell malloc_history pid mem”
Do I need to tell gdb to be in some mode?
Same here. I suspect it’s because we have old systems.
shell malloc_history pid addr
pours out a lot of addresses and tells me what I already know, that there’s an over-release involving some object the system/frameworks created, but it doesn’t point to what code of mine resulted in this side-effect.
I experimented a bit with variations:
(gdb) info malloc 0x57fe40
Undefined info command: “malloc 0x57fe40”. Try “help info”.
(gdb) info malloc-history 0x57fe40
Undefined info command: “malloc-history 0x57fe40”. Try “help info”.
(gdb) info malloc_history 0x57fe40
Undefined info command: “malloc_history 0x57fe40”. Try “help info”.
(gdb) malloc_history 0x57fe40
Undefined command: “malloc”. Try “help”.
(gdb) shell malloc_history 0x57fe40
*** malloc_history: incorrect syntax
malloc_history
Provides all the allocation history for process
Must have enabled recording by ‘setenv MallocStackLoggingNoCompact 1’
or:
malloc_history -all_by_size | -all_by_count
Must have enabled recording by ‘setenv MallocStackLogging 1’
Huge thanks man!
Amazing idea, but i got errors like the following where xxx is my project name until i remove the check from ‘MallocStackLoggingNoCompact’:
xxx(8804) malloc: unable to create stack log directory /tmp/stack-logs.8804.xxx.vWJUpP
So how can i debug without setting MallocStackLoggingNoCompact to 1?
I don’t know the answer, but maybe someone else who reads this does.
My concern, however, is that it seems to be a filesystem error. Is this a free space or permissions problem?
Thank you for your reply.
I’ve simply resolved the problem with creating a new project. My XIB files were broken.
Now it’s possible to use MallocStackLoggingNoCompact without problems.
regards
Does this work for the Debug with Device.
I am getting the error:
Thanks man! 🙂
Great help…
Thanks!
Two and a half years after you posted this blog entry, you’re still helping folks out. Thanks so much for helping me solve a problem that’s been dogging me for days.
I’m glad! I find some of these developer tips get the most consistent traffic!