At the end of last month, I posted an article about Cleansing your ActionScript codes; somewhere there seem I can still add a few more to it. I got few mails regarding the same, on my site log I even found people searching for externalRef
and also on MXNA. Read below and I hope the reference to externalRef
might also get cleared.
A discussion surfaced out of that article and I hope other people may benefit from the same. Components that extends
MovieClip, which are loaded and unloaded by the user, there have been issues and if we need to clean this up, do some kinda manual garbage collection!
So, can’t we just do it with an onUnload
event?
Well, onUnload
is fired After the movieclip has been removed. So it is better to remove external references before they are deleted. For instance if your movieclip(myMC) is subscribed to a component(myButton) that is outside it with addEventListener
, then myButton would have a reference to myMC. So before destroying myMC it would be better to delete the reference myMC in myButton by using removeEventListener
.
Similarly if you have an external reference to other outside objects in your class you need to remove them before you delete your movieclip.
For movieclips we can wrap all this into one dispose method that removes itself after deleting external references. So,
myMC.dispose ();
myMC.removeMovieClip ();
//
//would be shortened to
myMC.dispose ();
where myMC.dispose would also remove itself at the end.