Search code examples
flashactionscript-3actionscriptflash-cs4

null an object kill reference or the space in memory


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

Solution

  • 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.