views:

72

answers:

2

This might be a dumb question. I think I already know the answer, just clarify. If you declare a object in varible1 and then pass the value into varible2. If you decide to null varible2 would that kill just the reference or the object itself as well. I want to say no, but then again, everything you do to the reference it self, also affects the space in memory.

these are the 2 varibles in my class.

private var objects:Array;
private var viewableObjects:Array;

above are class varibles. Later on in my code I add an object to the objects array

objects[0][4] = new Enemy1();

When i trace i get the following

[object Enemy1]

I then add it to viewable objects array

viewableObjects.push(objects[0]);

next I remove it. this is later on down the lines. I am looping through the code that is my you see a "i" in the first element.

viewableObjects[i][4] = null;

and when I trace the same first varible "objects[0][4]"... it shows

null
+2  A: 

Setting a reference to null does not affect the object, unless it's the last reference to that object (in which case it makes it eligible for garbage collection)

You might want to read up some more on how references work.

Anon.
actually, I just test it out. I created 2 properties that was a custom class type. when I declared a object to varible1 and traced it, it showed the object existed. I then dumped it into varible2 and set it to null. I did a trace on varible1 and it showed null as well. could you explain what just happened there ?
numerical25
You're not being very clear. Could you post the code you used?
Anon.
Sorry, I updated my first post
numerical25
That's because you're confused about where the references actually point. `objects[0]` is the same as `visibleObjects[i]`. If you changed the reference - for example, said `visibleObjects[i] = null`, you wouldn't have any effect on `objects[0]`. But that's not what you're doing - you're changing the object itself.
Anon.
Ya, Anon's got it
Tyler Egeto
hmmm, I somewhat understand.
numerical25
+1  A: 

Hey, it definitely can be a little confusing, but it is one of those things you really must understand, so its good to ask.

In your situation, nulling variable 2 only removes the reference, the memory used will still be the same, (almost, the reference does take a very little bit), you have just reduced the reference count. It won't be cleaned up until all the references are removed, or all the references are isolated, and not used in the application. ie: the only object referencing it is available for garbage collection.

Tyler Egeto
I updated my code above. In it i have 2 arrays that I call. I then initiate a object in the first and push it into the second array. later , when i null the second one, and trace the first one, it shows as null. are arrays different. What you said makes sense but Im just not sure whats going on above.
numerical25
Can you post the complete code? You sample hints that their is really more going on. I'd like to see what is generating you "i" values, your trace statements, and anything else going on.
Tyler Egeto