This content has been marked as final. Show 4 replies
There won't be memory leaks if you make sure there are no more references pointing to that particular URLLoader, all event listeners to it are weak or removed after use, etc. The garbage collector will pick it up and free up memory.
But I would suggest re-using one (or two) instances of URLLoader since there's probably a bound on the upper number of simultaneous url requests. That is, you could make the calls simultaneously, but internally they would queue up after two requests have gone in. This is the behavior with most browsers (the RFC for HTTP suggests a maximum of two simultaneous socket connections).
But I have not confirmed this with the flash player or AIR. Even then, it would be best to have two or three URLLoader's that you re-use.
Thank you for the reply. I don't plan on doing multiple simultaneous connections, it will be one load at a time - but looping this hundreds or thousands of times, creating a new URLLoader and URLRequest each time raised some questions about the garbage collector.
Is there any way to quickly remove all event listeners without having to specify the function name(s)? I often use unnamed functions (I forget the correct terminology) for my event listeners as they may change dynamically.
Function closures. There's no way to remove all event listeners for a particular event, but you can use something like this in your anonymous function:
btn.addEventListener(MouseEvent.CLICK, function (event:Event):void
arguments.callee refers to the current function.
Note: If you set weak references as true for function closures, it might lead to unpredictable results:
In the case of function closures, setting useWeakReference to true as the last argument can lead to unexpected results. To Flex, an inner function is actually an object, and can be freed by the garbage collector. If you set the value of the useWeakReference argument to true, as shown in the previous example, there are no persistent references at all to the inner function. The next time the garbage collector runs, it might free the function, and the function will not be called when the event is triggered.
If there are other references to the inner function (for example, if you saved it in another variable), the garbage collector will not free it.
This is exactly what I needed. Thanks! :)