views:

37

answers:

0

I've recently been tasked with ferreting out some memory leaks in an application for my work. I've narrowed down one of the big leaks to a jquery plugin. It appears we're using a modified version of a popular context menu jquery plugin.

It looks like one of the developers before me attempted to add a destroy method. I noticed it wasn't very well written and attempted to rewrite. Here's the meat of my destroy method:

 if (menu.childMenus)
  {
   for (var i = 0; i < menu.childMenus.length; i++)
   {
    $(menu.childMenus[i]).destroy(menu.childMenus[i], 'contextmenu');
   }
  }

  var recursiveUnbind = function(node) {
         $(node).unbind();
         //$(node).empty().remove();
         $.each(node, function(obj) {
             recursiveUnbind(obj);
          });
        };
  $.each(menu, function()
  {
   recursiveUnbind(menu);
  });

  $(menu).empty().remove();

In my mind this code should blow away all the jquery event binding and remove the dom elements, yet still the plugin leaks gobs of memory in IE7.

The modified plugin with a test page can be found here:
http://www.olduglyhead.com/jquery/leaks/ Clicking the button repeatedly will cause IE7 to leak a bunch of memory.