Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ranguvar

macrumors 6502
Original poster
Sep 18, 2009
318
2
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?
 

rossipoo

macrumors regular
Jun 7, 2009
109
0
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?

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

ranguvar

macrumors 6502
Original poster
Sep 18, 2009
318
2
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?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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?

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.
 

ranguvar

macrumors 6502
Original poster
Sep 18, 2009
318
2
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.

Thanks, I now understand it.
 

slb

macrumors 6502
Apr 15, 2005
464
311
New Mexico
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.