http://www.ibm.com/developerworks/web/library/wa-memleak/ On this link, Listing 5 describes a situation where obj is a reference to a dom element, and the dom element has a reference to obj, and therefore causes a memory leak. I'm struggling to see what in the code makes the DOM element have a reference to obj. Could someone explain it to me?
Taken from the page:
In Listing 5 you see a closure in which a JavaScript object (obj) contains a reference to a DOM object (referenced by the id "element"). The DOM element, in turn, has a reference to the JavaScript obj. The resulting circular reference between the JavaScript object and the DOM object causes a memory leak.
Listing 5. Event handling memory leak pattern
<html>
<body>
<script type="text/javascript">
document.write("Program to illustrate memory leak via closure");
window.onload=function outerFunction(){
var obj = document.getElementById("element");
obj.onclick=function innerFunction(){
alert("Hi! I will leak");
};
obj.bigString=new Array(1000).join(new Array(2000).join("XXXXX"));
// This is used to make the leak significant
};
</script>
<button id="element">Click Me</button>
</body>
</html>