Finding freed/deallocated instances of objects

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

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.

17 thoughts on “Finding freed/deallocated instances of objects

  1. 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. 🙂

  2. 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?

    1. 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
      Provides all the allocation history for process
      Must have enabled recording by ‘setenv MallocStackLoggingNoCompact 1’
      malloc_history -all_by_size | -all_by_count
      Must have enabled recording by ‘setenv MallocStackLogging 1’

  3. 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/

    So how can i debug without setting MallocStackLoggingNoCompact to 1?

    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?

      1. 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.


  4. 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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s