Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created September 22, 2011 20:12
Show Gist options
  • Save abozhilov/1235894 to your computer and use it in GitHub Desktop.
Save abozhilov/1235894 to your computer and use it in GitHub Desktop.
Memory leak
// creates a script load listener
function create_script_load_listener(elem, registry_item, flag,onload) {
/**
* If the garbage collector use reference count for the host objects
* code below will eventually leak if `onload' or `onreadystatechange' hasn't triggered.
* `onload' and `onreadystatechange' create circular reference trought the internal [[Scope]]
* elem -> onload -> [[Scope]] -> Variable Object -> elem
*/
elem.onload = elem.onreadystatechange = function() {
if ((elem.readyState && elem.readyState != "complete" && elem.readyState != "loaded") || registry_item[flag]) return;
elem.onload = elem.onreadystatechange = null;
onload();
};
}
/*
* It's quite easy to fix that problem while after assigning `onload', `onreadystatechange'
* `elem' does not need to store reference to script object :)
*/
//Save way of creates script load listener
function create_script_load_listener1(elem, registry_item, flag,onload) {
elem.onload = elem.onreadystatechange = function() {
if ((this.readyState && this.readyState != "complete" && this.readyState != "loaded") || registry_item[flag]) return;
onload();
};
elem = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment