Professional Web Applications Themes

Cocoa memory trouble - Mac Programming

Although I think I have good handle on Cocoa memory managment, I still have some holes in my understanding. For example, in this code: NSEnumerator* e = [volumesArray objectEnumerator]; NSString* s = [[NSString alloc] init]; while(s = [e nextObject]){ [self setPathString:s]; [self addItemIfRemote:nil]; } // volumesArray exists and holds several strings This code will iterate a few times, and then crash with error 11. Stepping through the code, I see that the NSEnumerator and NSString are nil (0x0) just before the crash. Why would this happen? The array is not emtpy, there are still a few items left. I actually ...

  1. #1

    Default Cocoa memory trouble

    Although I think I have good handle on Cocoa memory managment, I still
    have some holes in my understanding. For example, in this code:

    NSEnumerator* e = [volumesArray objectEnumerator];
    NSString* s = [[NSString alloc] init];

    while(s = [e nextObject]){
    [self setPathString:s];
    [self addItemIfRemote:nil];
    }

    // volumesArray exists and holds several strings


    This code will iterate a few times, and then crash with error 11.
    Stepping through the code, I see that the NSEnumerator and NSString are
    nil (0x0) just before the crash. Why would this happen? The array is not
    emtpy, there are still a few items left. I actually do something similar
    elsewhere in my code, but with an instance variable instead of the local
    string. The enumerator is local in both methods and that works perfectly
    everytime. If I were just mishandling the NSString, it might make sense
    to me but I don't understand why the enumerator is nil. Can someone
    break this down to me. Meanwhile, I'm going to grab one of my Cocoa
    books.

    TIA
    Lorenzo Guest

  2. #2

    Default Re: Cocoa memory trouble

    In article
    <internal.thethurmans.com>,
    Lorenzo Thurman <om> wrote:
     

    There's a leak in the previous line.
     

    We need more context. Like what line is executing when you crash. If e
    is nil, [e nextObject] will be nil, so the problem may be simply that
    you're losing the enumerator somewhere along the line.

    G
    Gregory Guest

  3. #3

    Default Re: Cocoa memory trouble

    In article
    <internal.thethurmans.com>,
    Lorenzo Thurman <om> wrote:
     

    Sorry, I don't have the answer to your problem ... BUT you shouldn't
    allocate s. After all, the assignment statement in your loop condition
    immediately overwrites the pointer, which means you're leaking your
    NSString. This is not an ObjC mistake, but rather a straight C mistake.
    Pretty much any book on C describes how pointers work, and that you can
    easily leak pointers by overwriting one pointer with the other.

    So, I suppose your problem lies deeper than just a Cocoa memory
    management issue. You might want to dig out a good C book and revisit
    its chapter on pointers.

    Apart from that, it might help to see what you're doing in
    setPathString: and addItemIfRemote:. My spontaneous guess would be that
    either you have an object of the wrong type in one of your variables, or
    you haven't allocated one of the objects you're using, or releasing.
    Another option would be that either setPathString: or addItemIfRemote:
    are releasing volumesArray, or removing items from the array, which
    could cause your error. Remember, you haven't retained any of the
    objects you get back from nextObject, you only have pointers to the
    objects retained by the array. So, if you kill the array, your objects
    will suddenly go away.

    Apart from that, I'm out of guesses. Hope any of these help.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
    Uli Guest

Similar Threads

  1. Replies: 8
    Last Post: January 26th, 05:01 PM
  2. Cocoa + Xcode + GMP = What the?
    By M in forum Mac Programming
    Replies: 11
    Last Post: November 22nd, 11:46 PM
  3. Bit shifts in Cocoa
    By Korbin Meiser in forum Mac Programming
    Replies: 2
    Last Post: September 11th, 01:11 AM
  4. Cocoa Filemaker
    By Olly Groves in forum FileMaker
    Replies: 2
    Last Post: July 25th, 01:55 AM
  5. [Cocoa] Bug with NSTableView?
    By Mark Bee in forum Mac Programming
    Replies: 1
    Last Post: July 7th, 07:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139