[AS3]memory Leak for compressed image loaded with Loader

743 Views Asked by At

I am actually using the Loader Class to get an image from a distante server. I am using it for is job and when it's done I remove it from memory but there is a little leak.

To see it, I am using scout. In the memory part, the Bitmap DisplayObjects is free but the Images part is not.

When I look at the scout documentation, I found that the Images part is the compressed version of the files I am loading.

What should I do to free this part of the memory?

thanks

edit 2(forget the _ before loader): Here is what I did to free the Loader :

(_loader.content as Bitmap).bitmapData.dispose();
_loader.unload();
this.removeChild(_loader);
_loader = null;

edit 3 : I still need help for my memory leaks, thanks

1

There are 1 best solutions below

0
On

It's very easy to have a memory leak when using the Loader class. For the garbage collector to remove an object from the memory following rules must apply:

  • The object has no references
  • The object has no event listeners (or the event listeners have a WeakReference set to true)
  • The object is not included in any array

Here's a small example of using the Loader

var urlReq:URLRequest = new URLRequest("url here");

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
l.load(urlReq);

Looks pretty simple, doesn't it? ;) I did not include the handler, since it's not important for this example.

Now let's remove it:

l.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
//the LoaderInfo object is inside the Loader object, it needs to be free of listeners
//or it won't let the Loader object to be garbage collected

var content:DisplayObject = l.content; // save the loaded item
l = null; // make object NULL

Now the loader should be removed from the memory on the next garbage collector run. Just make sure you've cleared all the listeners and references.

Hope that helps!