I have that function in the class:
private function fireItemCreated(data: ByteArray): void {
setTimeout(function(): void {
var event: ItemCreatedEvent = new ItemCreatedEvent(data);
dispatchEvent(event);
}, 1000);
}
This function called to dispatch item created event when image thumbnail created.
But it delays event on some time to prevent user interface freezes. And I’m guessing what could be happen if garbage collector executes after fireItemCreated function call but before timer event. Does the closure will be removed or it stays until it will be executed?
It can’t happen.
If the function is called then
setTimeoutis called. The function-object passed tosetTimeoutcreates a strong closure-binding with the linked execution context and allsetTimeoutcallback functions are protected (strongly held) by the host engine (imagine there is an invisiblevar timeouts = []you can’t access). It wouldn’t be fun if timers were magically swallowed up by the evil Grime Captain.Good question and Happy coding.
The issue described can actually happen in some other languages and their implementations of Timers. See .NET’s Threading.Timer Class and the notes.