Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 7, 2009, 08:56 AM   #1
ranguvar
macrumors 6502
 
Join Date: Sep 2009
NSMutableArray & garbage collection

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?
ranguvar is offline   0 Reply With Quote
Old Nov 7, 2009, 09:27 AM   #2
rossipoo
macrumors regular
 
Join Date: Jun 2009
Quote:
Originally Posted by ranguvar View Post
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.
rossipoo is offline   0 Reply With Quote
Old Nov 7, 2009, 09:34 AM   #3
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Nov 7, 2009, 10:20 AM   #4
ranguvar
Thread Starter
macrumors 6502
 
Join Date: Sep 2009
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?
ranguvar is offline   0 Reply With Quote
Old Nov 7, 2009, 10:32 AM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Nov 7, 2009, 12:31 PM   #6
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by ranguvar View Post
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.
gnasher729 is offline   0 Reply With Quote
Old Nov 7, 2009, 02:14 PM   #7
ranguvar
Thread Starter
macrumors 6502
 
Join Date: Sep 2009
Quote:
Originally Posted by gnasher729 View Post
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.
ranguvar is offline   0 Reply With Quote
Old Nov 9, 2009, 03:40 PM   #8
slb
macrumors 6502
 
Join Date: Apr 2005
Location: New Mexico
Send a message via AIM to slb Send a message via MSN to slb
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.
slb is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
I need SSD, TRIM and Garbage Collection update Doc69 Mac Pro 19 Jan 3, 2013 07:28 PM
Fusion Drive - Over Provisioning, Garbage Collection, TRIM Dadioh Mac mini 35 Dec 11, 2012 09:21 AM
2.3 i7 with 830 samsung. Is it ok regarding Garbage collection? mystic man Mac mini 1 Dec 6, 2012 11:49 AM
2 Questions: Garbage collection & SD Card slot hkim1983 MacBook Pro 6 Jun 17, 2012 08:48 PM

Forum Jump

All times are GMT -5. The time now is 01:27 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC