05
May
09

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: 0×92643782 in calloc

    2: 0×93611618 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.

About these ads

17 Responses to “Finding freed/deallocated instances of objects”


  1. May 22, 2009 at 7:35 am

    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

  2. July 8, 2009 at 6:02 am

    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. :)

  3. 3 Gareth
    July 28, 2009 at 11:59 pm

    Thank you thank you thank you.

  4. August 7, 2009 at 11:53 am

    Thank you! You just saved my life — I had been looking in all the wrong places until I found this debugging aid.

  5. 5 Chris Palmer
    January 29, 2010 at 9:31 pm

    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?

    • 6 jgo
      September 15, 2010 at 8:07 pm

      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′

  6. May 31, 2010 at 10:04 am

    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?

    • 9 dkavanagh
      June 1, 2010 at 1:26 pm

      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?

      • June 1, 2010 at 3:00 pm

        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

  7. 11 erastus
    January 13, 2011 at 11:33 am

    Does this work for the Debug with Device.
    I am getting the error:

  8. 12 Paki
    February 26, 2011 at 3:38 pm

    Thanks man! :)

  9. 14 Lynn Gobin
    October 18, 2011 at 7:49 pm

    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:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: