Project Elbow: File Manager / Weak References to BitmapDatas in Actionscript Flash.Utils.Dictionary

The FileManager was written to run FileManagerTasks which run FileLoaders.    The FileManager keeps a queue of tasks waiting to run and a list of the tasks running.  The FileManager will not run another task if there are too many already running.  It also won’t run a task if we’re already loading something with the same name or ID.

The FileLoader class checks first to see if we have the file on disk.  If not, the file is downloaded and put on disk.

The FileLoader returns either the content as an array of bytes (or a UTF8 string), or it loads the content using the Flash Loader in the case of images and SWF files.

So here’s the rub: If you ask FileManager for a file and then ask for it again later and the original is still in memory, then why not just create a new DisplayObject from the one already in memory.  For instance, if you’ve loaded an image, and you need another of that image, then you should just create a Bitmap from the BitmapData of the Bitmap already in the display list.

AND, SO, FINALLY, FileManager was upgraded to keep a Dictionary whose keys are weak references to the BitmapData references of the Bitmap DisplayObjects that were loaded.

In case you’ve found this blog post because you’re wanting or think you’re wanting to mess with weak references, then I’ll put a snipped of code below, but I suggest you read the following before you panic yourself into thinking you always need to use weak references.

ActionScript 3.0 Events: The Myth of useWeakReference

So, anyway, in the FileManager class, I keep a list of bitmapdata’s in this:
 protected static var loadedBitmaps:Dictionary = new Dictionary(true);

After a Bitmap is loaded, I save a weak reference to its BitmapData, like this:
if ( task.loader.content is Bitmap )
{
var bitmap:Bitmap = task.loader.content as Bitmap;
loadedBitmaps[bitmap.bitmapData] = task.id_or_filename.toString();
}
And before I ask the FileLoader to actually load something from disk or download, I check that Dictionary object to see if that sucker is already in memory, like this:
for ( var ref:* in loadedBitmaps )
{
if ( loadedBitmaps[ref] == task.id_or_filename.toString() )
{
var event:FileManagerCallbackEvent = new FileManagerCallbackEvent();
event.id_or_filename = task.id_or_filename;
event.status = FileManagerCallbackEvent.STATUS_COMPLETE;
event.content = new Bitmap( ref as BitmapData );
task.callback(event);
processQueue(); // See if there's anything left to do.
return;
}
}
When an object is garbage collected, entries in a Dictionary of weak references that point to objects that are gone will be removed.  And a weak reference will not cause the object to be held in memory as though it’s in use (that’s why they call the reference “weak”).
See:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Dictionary.html#Dictionary()

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.