Is this jQuery code snippet causing a memory leak?

I am wondering if the following jquery code is causing a memory leak:

$( function() { var parent=$('table#mytable tbody').get(0); $('tr:last', parent).click(function(){...}); }); 

In my understanding, $('tr:last', parent) is the last line that is a DOM object, but in an anonymous function, the closure has this DOM object in scope, so there is a circular link between the DOM and js objects.

But if it does have a leak, then I see that in the popular jQuery in Action book published by Manning there are many such types of code. Is this a harmful "best practice" in jQuery coding?

But I do not know if I understand correctly. Hope your comments and corrections. Thanks!

+4
source share
3 answers

This code will cause a memory leak in Internet explorer 6 and Internet explorer 7 in a different browser, please list these arithmetic Douglas Crockford and IBM Devloper Site

+2
source

IE is a tendency to leak. You can test your code for leaks in IE using memory leak detection in Microsoft javascript .

Firefox may also leak. This article explains why. There is also a leak detector plugin for Firefox (not tested it yet).

+1
source

Previously submitted answers (from 3 years ago, sighs) are erroneous. While jQuery 1.0 - 1.2 will flow here, looking at the version of jQuery this question was asked from http://download.oldapps.com/jquery/jquery-1.3.js , the following line was included in the function, which adds all event listeners:

  // Nullify elem to prevent memory leaks in IE elem = null; 

This is an accurate suggestion for preventing leakage from related documentation.

0
source

All Articles