finding memory leaks

  • ...surely a FAQ but I am a bit struggling with it over this code

      http://nopaste.ch/1685f5d17a92cfd.html

    For this snippet there is nothing that should live on after the
    blocks has finished. So no "retain" and things should be quite
    straight forward. With the NSAutoreleasePool I just find it hard to
    find out what needs to the "autorelease"'d and what not. ("alloc",
    "new" etc are already covered - I know) But somehow I am still
    failing according to "leaks".

    Call stack: [thread 1dad]: | 0x1 | start | _start | main |
    NSApplicationMain | -[NSApplication run] | -[NSApplication
    sendEvent:] | -[NSWindow sendEvent:] | -[NSTextView keyDown:] | -
    [NSView interpretKeyEvents:] | -[NSTSMInputContext
    interpretKeyEvents:] | -[NSKeyBindingManager
    (NSKeyBindingManager_MultiClients) flushTextForClient:] | -
    [NSTextView insertText:] | -[NSTextView(NSSharing) didChangeText] | -
    [NSNotificationCenter postNotificationName:object:userInfo:] |
    _CFXNotificationPostNotification | __CFXNotificationPost |
    _nsnote_callback | -[NSTextField textDidChange:] | -
    [NSNotificationCenter postNotificationName:object:userInfo:] |
    _CFXNotificationPostNotification | __CFXNotificationPost |
    _nsnote_callback | -[MyDocument controlTextDidChange:] |
    CFArrayAppendValue | _CFArrayReplaceValues | malloc_zone_malloc
    Leak: 0x003c9780  size=32    string ' '

    Call stack: [thread 1]: | 0x1 | start | _start | main |
    NSApplicationMain | -[NSApplication run] | -[NSApplication
    sendEvent:] | -[NSWindow sendEvent:] | -[NSTextView keyDown:] | -
    [NSView interpretKeyEvents:] | -[NSTSMInputContext
    interpretKeyEvents:] | -[NSKeyBindingManager
    (NSKeyBindingManager_MultiClients) flushTextForClient:] | -
    [NSTextView insertText:] | -[NSTextView(NSSharing) didChangeText] | -
    [NSNotificationCenter postNotificationName:object:userInfo:] |
    _CFXNotificationPostNotification | __CFXNotificationPost |
    _nsnote_callback | -[NSTextField textDidChange:] | -
    [NSNotificationCenter postNotificationName:object:userInfo:] |
    _CFXNotificationPostNotification | __CFXNotificationPost |
    _nsnote_callback | -[MyDocument controlTextDidChange:] | -
    [NSPlaceholderMutableArray init] | -[NSPlaceholderMutableArray
    initWithCapacity:] | __CFArrayInit | _CFRuntimeCreateInstance |
    malloc_zone_malloc
    Leak: 0x003a6760  size=16    string '@/0'

    Call stack: [thread 1]: | 0x1 | start | _start | main |
    NSApplicationMain | -[NSApplication run] | -[NSApplication
    sendEvent:] | -[NSWindow sendEvent:] | -[NSTextView keyDown:] | -
    [NSView interpretKeyEvents:] | -[NSTSMInputContext
    interpretKeyEvents:] | -[NSKeyBindingManager
    (NSKeyBindingManager_MultiClients) flushTextForClient:] | -
    [NSTextView insertText:] | -[NSTextView(NSSharing) didChangeText] | -
    [NSNotificationCenter postNotificationName:object:userInfo:] |
    _CFXNotificationPostNotification | __CFXNotificationPost |
    _nsnote_callback | -[NSTextField textDidChange:] | -
    [NSNotificationCenter postNotificationName:object:userInfo:] |
    _CFXNotificationPostNotification | __CFXNotificationPost |
    _nsnote_callback | -[MyDocument controlTextDidChange:] | -
    [NSPlaceholderMutableArray init] | -[NSPlaceholderMutableArray
    initWithCapacity:] | __CFArrayInit | _CFRuntimeCreateInstance |
    malloc_zone_malloc
    Leak: 0x003a5e50  size=16    instance of 'ABSearchElementConjunction'
    0xa47d2f20 0x00000001 0x003c9140 0x00000000  /}.....@.<.....

    I need some help here to interpret this output. I have absolutely no
    clue where the leaking strings are coming from. First though
    "stringByTrimmingCharactersInSet" (line 14) but that does not like to
    be autorelease'd. And for the ABSearchElementConjunction that looks
    like searchElementForConjunction:children (line 47) needs an
    autorelease. But when I do add it - the app crashes.

    Any help appreciated.

    cheers
    --
    Torsten
  • On Feb 11, 2008, at 09:25, Torsten Curdt wrote:

    > For this snippet there is nothing that should live on after the
    > blocks has finished. So no "retain" and things should be quite
    > straight forward. With the NSAutoreleasePool I just find it hard to
    > find out what needs to the "autorelease"'d and what not. ("alloc",
    > "new" etc are already covered - I know) But somehow I am still
    > failing according to "leaks".

    It looks like you're leaking leaking your "queries" NSMutableArray.
    The autorelease pool you established is only responsible for cleaning
    up autoreleased objects, so you need to add in a "[queries release]"
    statement somewhere (or use "queries = [NSMutableArray array]" instead).

    You also shouldn't be autoreleasing the result of the
    "searchElementForProperty" messages. This method is (by convention)
    already returning an autoreleased object.

    /brian
  • On 11.02.2008, at 15:45, Brian Christensen wrote:

    > On Feb 11, 2008, at 09:25, Torsten Curdt wrote:
    >
    >> For this snippet there is nothing that should live on after the
    >> blocks has finished. So no "retain" and things should be quite
    >> straight forward. With the NSAutoreleasePool I just find it hard
    >> to find out what needs to the "autorelease"'d and what not.
    >> ("alloc", "new" etc are already covered - I know) But somehow I am
    >> still failing according to "leaks".
    >
    > It looks like you're leaking leaking your "queries" NSMutableArray.
    > The autorelease pool you established is only responsible for
    > cleaning up autoreleased objects, so you need to add in a "[queries
    > release]" statement somewhere (or use "queries = [NSMutableArray
    > array]" instead).
    >
    > You also shouldn't be autoreleasing the result of the
    > "searchElementForProperty" messages. This method is (by convention)
    > already returning an autoreleased object.

    Thanks, Brian! That fixed it. I think I need to do a bit more
    homework in this area.

    cheers
    --
    Torsten