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.