Created
September 22, 2011 20:12
-
-
Save abozhilov/1235894 to your computer and use it in GitHub Desktop.
Memory leak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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