Hedger Wang has been scanning a lot of Chinese blogs lately for solutions to IE6 and memory leak issues. One of the things he stumbled upon is a pretty nifty way of nulling the objects to stop memory leaks by using the try ... finally construct. So instead of this solution which leaks memory:
JAVASCRIPT:
-
-
function createButton() {
-
var obj = document.createElement("button");
-
obj.innerHTML = "click me";
-
obj.onclick = function() {
-
//handle onclick
-
}
-
obj.onmouseover = function() {
-
//handle onmouseover
-
}
-
return obj;//return a object which has memory leak problem in IE6
-
}
-
var dButton = document.getElementsById("d1").appendChild(createButton());
-
//skipped....
-
You can use the following which doesn't:
JAVASCRIPT:
-
-
function createButton() {
-
var obj = document.createElement("button");
-
obj.innerHTML = "click me";
-
obj.onclick = function() {
-
//handle onclick
-
}
-
obj.onmouseover = function() {
-
//handle onmouseover
-
}
-
//this helps to fix the memory leak issue
-
try {
-
return obj;
-
-
} finally {
-
obj = null;
-
}
-
}
-
var dButton = document.getElementsById("d1").appendChild(createButton());
-
}
-
More demos, proof of concept examples and the "finally" explanation is available on Hedger's blog: Finally, the alternative fix for IE6's memory leak is available