Consider the following segment of code:
function loadSomeContent()
{
URLLoader loader = new URLLoader("http://www.somesite.com/");
loader.addEventListner("onLoadComplete", loadCompleteHandler);
loader.sendHttpRequest();
}
function loadCompleteHandler(event)
{
log("Load response received");
}
Do not worry about the syntax of this code.
Here is my concern – The loader object which is used to send the HTTP request and which has the onLoadComplete event registered to is not referenced from outside the loadSomeContent() function. Is there a possibility that the loader object will be garbage collected and loadCompleteHandler() will never be called?
When you call
loader.sendHttpRequest()a new thread is created that will actually send the request in the background. This thread keeps a reference to the loader so that it can call the load complete function when the load is finished. As a result the loader will always be referenced by some thread, just not the thread you’re in right now.