image ads

Monday, October 18, 2010

stupid XML in Flash Lite Digital Home

For Flash Lite Digital Home's developers, working with XML really is a nightmares;

If you are not aware of it, it will make your application stop functioning, part of your data will lost;
If you are not aware of it, it will make your device crash and the memory leak issue will appeal without your notice.

Normally developer will think that delete the XML object created or set that XML object to null then the Garbage Collector will auto collect it;

Unfortunately, it work another way round.
As we know, the Garbage Collector in Flash Lite was using reference count method.
This mean if a variable the reference count not equal to zero, the Garbage Collector will not collect it.

For this XML case, somehow a global variable in Adobe XML class, "idMap" hold a reference of the XML object itself. This make the reference count never become zero, so the Garbage Collector will not auto collect it.

This will make the memory leak issue happen in your device at the end!
This will make your application running slow, when you try to exit your application, it will take more long time as usual. Sometime will reach 5minutes!!!

So must remember delete the variable "idMap" before delete the XML object.

Here is some sample code:

1. For global XML variable
import mx.utils.Delegate;
class test{
private var xml:XML = null;

private function loadXML():Void
{
this.xml = new XML();
this.xml.ignoreWhite = true;
this.xml.onLoad = Delegate.create(this, this.XMLonLoad)
this.xml.load(url);
}

private function XMLonLoad(success:Boolean):Void
{
trace(this.xml);
//work with xml data
delete this.xml.idMap;
delete this.xml;
this.xml = null;
}

}


2. Local XML variable
import mx.utils.Delegate;
class test
{
private function loadXML():Void
{
var xml = new XML();
xml.ignoreWhite = true;
xml.onLoad = Delegate.create(this, function(success:Boolean):Void
{
trace(this.xml);
//work with xml data

delete xml.idMap;
xml = null;
});
xml.load(url);
}
}

No comments: