Jump to content
GreenSock

lukaMis

Question about garbage collection

Recommended Posts

Hi

 

 

I have a question about SWFLoader. Do i need to remove any event listeners in onCompleteHandler? I was used to write my code like that

function onCompleteHandler(e:Event):void
{
loader.removeEventListener(Event.COMPLETE, onCompleteHandler);
loader = null;
}

 

And i wander what do i have to do when using SWFLoader and LoaderMax.

 

tnx

hf

Luka

Link to comment
Share on other sites

It depends. Keep in mind that LoaderMax keeps internal references to all loaders to protect them from garbage collection and make it possible to do global searches by name, like LoaderMax.getLoader("myLoaderName"). That is, until you dispose() the loader. If you add listeners using the built-in shortcut way like this:

 

var loader:SWFLoader = new SWFLoader("child.swf", {onComplete:completeHandler, onProgress:progressHandler});

 

Then you don't need to explicitly remove them because the dispose() method will automatically remove the listeners for you. But if you add them manually like this:

 

var loader:SWFLoader = new SWFLoader("child.swf");
loader.addEventListener(LoaderEvent.COMPLETE, completeHandler);
loader.addEventListener(LoaderEvent.PROGRESS, progressHandler);

 

Then you DO need to remove them in order to make the loader eligible for garbage collection. Don't forget to call dispose(), of course.

 

And there's an autoDispose special property for the vars parameter that you can set to true and then it'll call dispose() automatically right after the loader dispatches its COMPLETE event, so this can reduce the hassle even further.

 

Does that answer your question?

Link to comment
Share on other sites

Hi

 

Well it does to some extent. What is the best practice for using LoaderMax and/or SWFLoader regarding garbage collection. Is it best to use autoDispose to get rid of loader after it has done its job, or is it better to keep it around? :)

I am building a website and garbage collection is very important for me for this project as it must function as smoothly as possible. (for that reason i am using greenSock tween engine, it the best :))

 

Luka

Link to comment
Share on other sites

It depends on your needs. Some applications need to keep loaders around so that they can check their status, find out how many bytesTotal there are, etc. Some developers need to be able to look up loaders to get their content well after they're done loading with LoaderMax.getContent() or LoaderMax.getLoader(). VideoLoader and MP3Loader even have media playback functionality built right into the loader! If you set autoDispose:true on those, it would prevent you from being able to use them to control the media. So it's not a matter of "you should always dispose() your loaders right away". You decide as the developer if you need to keep them around or not. If not, dispose() them.

 

Make sense?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×