views:

2171

answers:

2

I have a 'framework' in Flex which loads and destroys child 'sections', which are instances of module classes. These have a lot of webservice and animation in them and are part of a public facing site.

Before I remove a section from the screen I call a 'hideSection()' interface method on the instance. In this method I fade out any controls, or return false if the section wants to prevent itself from being closed. Currently it also stops any Timer instances running.

The problem is that even with the section object removed from the stage there may be outstanding things left to happen. For instance I may have an effect running where effectEnded triggers something, or perhaps a slow webservice request might timeout and cause a error to popup.

Because of the way the garbage collector works - sometimes that object object gets killed off sooner, and other times later. I'm trying to minimize bad things happening once a section has been closed.

I've come up with the following possible solution. Wondered if there was a better one.

  • Have a _disposed property which is set to true. Inside any event handler that could possibly have undesired behavior (after the section is closed) I would just say if (_disposed) { return; }.
  • May also be necessary to implement an 'IDisposable' interface, like in .NET.

Is this really my only option - or can i somehow expedite the garbage collection. Could garbage collection even happen if there were effects still running?

I'm also curious as to whether I should set things to _null, especially timers. Or is it sufficient to just stop() a timer to get it to be garbage collected if there are no references left to it.

A: 

You could remove all active event listeners in the hideSection() method.

removeEventListener(this, listenerFunction, eventType);

If you added the event listener with a weak reference, and have no other references to it, the target of the listener should be garbage collected, then.

moritzstefaner
+1  A: 

http://gskinner.com/talks/resource-management/

this is grand skinner's talk about garbage collection. Around slide 32 he talks about his janitor system. you can read over then and then grab his source files.

also make sure you are familiar with his talk about flash 9's garbage collection: http://www.gskinner.com/blog/archives/2008/04/failure_to_unlo.html

specifically look at this part:

Work-Arounds and Strategies There are four main things you can do to address these issues now:

  1. Make sure that you are always removing timer and enterframe event listeners in content you may want to load into a larger application. Also, try to avoid stage listeners where possible, and remove them immediately when you are done with them.

  2. Expose a standard API in your SWFs that allows other SWFs to tell it to clean up and stop executing. This way, the loading application can call this method (within a try/catch block) before it unloads any content. I would suggest a .halt() method, backed by a listener for a "halt" event through sharedEvents.

  3. You can load content SWFs from a subdomain. This will place it into a security sandbox implicitly.

  4. Load content into a div layered over your main application. This isn't a great option, but it addresses almost all of the issues.

gltovar