Posted by Philip Bulley | Filed under Actionscript 3
It’s always advisable to use weakly referenced event listeners in AS3. If you need to recap on why, check out Grant Skinner’s blog post. But in practice, if you’re ‘coding at the speed of thought’, or if you’ve inherited someone else’s code base, you may end up with some calls to addEventListener() which don’t use a weak reference.
The following regular expression will find all occurrences of addEventListener() that don’t use a weak reference:
addEventListener( *)\([a-zA-Z._ ]*,[a-zA-Z._ ]*\)
Using this with your code editor’s “Find in Files…” feature (ensure you check the “Use Regular Expression” option) will track down each offending code snippet, which you can then manually amend should you decide that using a weak reference is suitable.
Just make sure that you’re not relying on the reference created by addEventListener() to prevent the garbage collector from devouring your object for dinner. Generally, using a weak reference is fine when adding an event listener to a class scoped object, but not to an object locally defined within your current method.
Feel free to test/demo/fork it at http://regexr.com?2s5fc