Jquery plugin seems to leak memory no matter what I do

Posted by ddombrow on Stack Overflow See other posts from Stack Overflow or by ddombrow
Published on 2010-05-27T16:22:35Z Indexed on 2010/05/28 0:01 UTC
Read the original article Hit count: 145

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery