> delete something that garbage collection would take over after the 
> function exits. Someone correct me if I'm wrong.

Yes, you can do that with Flash methods like onEnterFrame, but not objects.

Notice, just because you delete an object like that and it doesn't respond
to methods that cause a trace event doesn't mean it's gone.  It's just
orphaned (has no reference) but it will never get collected.

If you want to see an example of this.

function loadMyXML() {
        myXML = new XML();
        myXML.onLoad = function() {
                trace("xml loaded");
                delete this;
        }
        myXML.load("someurl.xml");
}

BTN_Loader.onRelease = function() {
        trace("myXML = " + myXML.onLoad);
        loadMyXML();    
};

Click the button after each time the xml is loaded.  You will see that myXML
cannot delete itself because the trace on the button spits out [type
Function].

So, let's take it to the next step, and create a for loop that will delete
any completed XML objects.

this.xmlHolder = {};
//
function loadMyXML() {
        for (var i in this.xmlHolder) {
                if (this.xmlHolder[i].complete) {
                        delete this.xmlHolder[i];
                }
        }
        var id = new Date().getTime();
        this.xmlHolder[id] = new XML();
        this.xmlHolder[id].ignoreWhite = true;
        this.xmlHolder[id].onLoad = function() {
                trace("xml loaded");
                this.complete = true;
        };
        this.xmlHolder[id].load("someurl.xml");
}
BTN_Loader.onRelease = function() {
        var n = 0;
        for (var a in xmlHolder) {
                n++;
        }
        trace("xmlObjs = " + n);
        loadMyXML();
};


When you click the button, you will see 0, and then 1 every time.  Looks
like it is working, right?  The object and the reference are both getting
deleted.

However, if you continue to do this, you will see your memory usage continue
going up slowly over time.  Yes, the XML object is getting deleted, but the
onLoad function you assigned is being orphaned and thus is not getting
cleaned up by the garbage collector.  So, you have to explicitly delete the
onLoad function of the xml object before you delete the xml object.

for (var i in this.xmlHolder) {
        if (this.xmlHolder[i].complete) {
                delete this.xmlHolder[i].onLoad;
                delete this.xmlHolder[i];
        }
}

Now you have no memory waste.  It's really only an issue with apps you
expect to continue running for quite some time.  The onLoads that pile up
constitute only a little bit each time, but it does add up.

HTH,
Steven

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to