Garbage collection question
Consider the following segment of code:
function loadSomeContent()
{
URLLoader loader = new URLLoader("http://www.somesite.com/");
loader.addEventListner("onLoadCom开发者_运维知识库plete", 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.
No. Not as long as the URLLoader contains a callback that is still referenced somewhere (in this case, it's still referenced by the Javascript Engine/JVM/whatever because the Engine has to call it later.
In my experience, an active loader object such as URLLoader
or Loader
won't be collected (while it's active).
However, an important point that shouldn't be missed is that this an observable behavior, at best, but it's not documented as such (at least, to my knowledge; I might be wrong).
So, despite the internal mechanism used by the player to implement this (whether it creates a new thread, uses a centralized and global connection pool, etc, etc), I think you should not rely on this, because you'd be tying your code to an implementation detail (again, if this behavior is documented, this does not apply!).
Also, as I mentioned in a comment, there's a class in the flash.net
API, FileReference
that could be collected if you used it as in your code.
So, to sum it up: you can probably get away with just storing a local reference based on how things currently work (and based on observation!) but this is not guaranteed, so you're better off storing a non-local reference while your loader is active.
only if:
function loadCompleteHandler(e: event): void{
(e.target as URLLoader).removeEventListner("onLoadComplete", loadCompleteHandler);
}
it should be collected by AS3 GC (so that there are no references left and so we can't check if this local var exists:)
in actionscript the loader will not get garbage collected. The event handler is still registered as a link since you didn't add a weak reference to it. I actually do this quite often for loading, I like it since you don't have to store arrays of objects to deal with references.
This would break if you add a weak reference:
loader.addEventListner("onLoadComplete", loadCompleteHandler, false, 0, true);
You may have memory issues if you don't clean your reference in the loadCompleteHandler;
function loadCompleteHandler(event) {
(event.currentTarget as URLLoader).removeEventListener('onLoadComplete');
}
精彩评论