outaTiME

at devel days

Is “finally” the answer to all IE6 memory leak issues?

leave a comment »

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:

  1.  
  2. function createButton() {
  3.       var obj = document.createElement(“button”);
  4.       obj.innerHTML = “click me”;
  5.       obj.onclick = function() {
  6.         //handle onclick
  7.       }
  8.       obj.onmouseover = function() {
  9.         //handle onmouseover
  10.       }
  11.       return obj;//return a object which has memory leak problem in IE6
  12.     }
  13.     var dButton = document.getElementsById(“d1″).appendChild(createButton());
  14.     //skipped….
  15.  

You can use the following which doesn’t:

JAVASCRIPT:

  1.  
  2. function createButton() {
  3.       var obj = document.createElement(“button”);
  4.       obj.innerHTML = “click me”;
  5.       obj.onclick = function() {
  6.         //handle onclick
  7.       }
  8.       obj.onmouseover = function() {
  9.         //handle onmouseover
  10.       }
  11.       //this helps to fix the memory leak issue
  12.       try {
  13.         return obj;
  14.  
  15.       } finally {
  16.         obj = null;
  17.       }
  18.     }
  19.     var dButton = document.getElementsById(“d1″).appendChild(createButton());
  20. }
  21.  

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

(Via Ajaxian.)

Written by outaTiME

June 10, 2008 at 1:22 am

Leave a Reply