Regex to find non-weak referenced addEventListener()

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:

1
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.

FDT search regex to find non-weak referenced addEventListener code

Example of using the regular expression in the FDT "File Search" dialogue

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

Tags: , ,

Leave a Reply