NSMutableArray & garbage collection

Discussion in 'Mac Programming' started by ranguvar, Nov 7, 2009.

  1. macrumors 6502

    Joined:
    Sep 18, 2009
    #1
    Hi,

    just a quick question: When using garbage collection, does calling the insertObject:atIndex: method copy the object to be inserted?
    So, after having inserted my object into the array, can I do:
    Code:
    myObject = nil
    so that the garbage collection can deallocate it?
    Or will the NSMutableArray just keep a reference to my object, thus meaning I cannot set myObject to nil?
     
  2. macrumors regular

    Joined:
    Jun 7, 2009
    #2
    All mutable containers retain objects added to them. This happens regardless of garbage collection. It's not a copy, but a reference to the object is held by the array to make sure the object is not deallocated, so it's not necessary to keep track of them yourself.
     
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Any particular pointer is NOT the object. When you add an object to an NSMutableArray, its retain count is incremented. You shouldn't nullify a pointer without sending release, unless the object is already autoreleased. If you do nullify a pointer, you've lost that reference, but the object is still fine, sitting out on the heap. If it's the last reference to an object, and you have ownership (not autoreleased), you'll leak memory since you have no way to send a release.

    Basically, adding your object to a cocoa data structure will result in the proper retain behavior(retain on add, release on remove or release of the structure), so you can relinquish control with release and "forget" your reference by nullifying the pointer.

    I just wanted to point out that adding something to an NSMutableArray will retain it, not copy it. Also, nullifying a pointer does nothing to the object. So it is safe to nullify after an add to an array, but only after relinquishing ownership.

    -Lee

    -Lee
     
  4. thread starter macrumors 6502

    Joined:
    Sep 18, 2009
    #4
    Correct me if I'm wrong: NSMutableArray just retains my object, and because GC takes care of retain counts, I do not have to nullify my object after adding it to the NSMutableArray. Right?
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    You can't nullify an object. If you're using GC, you need to nullify pointers to objects if you are done using them (if they stay in scope) so the GC system knows you're done using them and they can be reaped. As long as your NSMutableArray is in scope, it will have a reference to your object, so your object will not be reaped.

    -Lee
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    The short answer: I don't have a clue what you mean.

    The long answer:

    You start with a pointer to an object. The garbage collector won't free the object because there is a valid pointer to it.

    You add that pointer to the mutable array. There are now two pointers to the object: Your original variable and a pointer stored in the mutable array.

    If you set the pointer variable to nil, then there is only one pointer to the object left. One is more than zero, so the object cannot be free'd by the garbage collector. If you leave the function that contains the pointer variable, the same thing happens.

    Once a pointer the object is in the mutable array, the object can only be freed by the garbage collector if you either remove the pointer from the array, or the array itself is freed.
     
  7. thread starter macrumors 6502

    Joined:
    Sep 18, 2009
    #7
    Thanks, I now understand it.
     
  8. slb
    macrumors 6502

    Joined:
    Apr 15, 2005
    Location:
    New Mexico
    #8
    Setting a pointer to nil in a garbage collected environment is unnecessary unless you know the pointer is going to stick around for a while, such as if it's an instance variable of the class. If it's an instance variable, you should use an accessor to clear it (e.g., [self setMyObject:nil]). Otherwise, if the pointer is a local variable, it won't exist outside its scope anyway, so manually nullifying it is pointless.

    If you don't know C pointers, you need to learn about them before you continue. It sounds like you don't understand what they are.
     

Share This Page