On a very specific note: memory leaks can become rampant in your code when using EventListeners
. The most common example I've seen in AS/Flex tutorials for adding listeners looks like this:
button.addEventListener(MouseEvent.CLICK, doSomething);
This works just fine, but ignores one salient point: the listener is strongly referenced. This means when the component containing this button is GC'd, the listener persists and maintains a reference to the button, meaning it also won't be harvested.
To mitigate this, you can do one of two things:
button.addEventListener(MouseEvent.CLICK, doSomething, false, 0, true);
Here is Adobe's description of the 3 extra fields. Note what they say about strong references:
A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.
The other option is to create a destructor in your code so when a component which uses EventListeners
removes them before being torn down:
button.removeEventListener(MouseEvent.CLICK, doSomething);