How to keep track of objects for garbage collection

Posted by Yman on Stack Overflow See other posts from Stack Overflow or by Yman
Published on 2010-04-28T17:29:02Z Indexed on 2010/04/28 18:17 UTC
Read the original article Hit count: 259

Filed under:
|
|

May I know what is the proper way to keep track of display objects created and hence allow me to remove it efficiently later, for garbage collection. For example:

for(i=0; i<100; i++){
var dobj = new myClass(); //a sprite
addChild(dobj);
}

From what i know, flash's garbage collection will only collect the objects without strong references and event listeners attached to it.

Since the var dobj is strongly referenced to the new object created, I will have to "nullify" it too, am I correct?

Should I create an array to keep track of all the objects created in the loop such as:

var objectList:Array = new Array();    

for(i=0; i<100; i++)
        {
            var dobj = new myClass(); //a sprite
            addChild(dobj);
            objectList.push(dobj);
        }

//remove all children
for each (var key in objectList)
{
     removeChild(key as myClass);
}

Does this allow GC to collect it on sweep?

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about flash