views:

1116

answers:

2

I am wondering if the following jquery code causes any memory leak:

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

For my understanding, $('tr:last', parent) is the last row which is the DOM object, but in the anonymous function, the closure has this DOM object in scope, so there is a circular reference between DOM and js objects.

But if it really has leak, then I can see there are many such kind of code in the popular book "jQuery in Action" published by Manning. It is harmful "best practice" in jQuery coding?

But i don't know if my understanding is correct. I hope your comments and corrections. Thanks!

+3  A: 

This code will cause memory leak in internet explorer 6 and internet explorer 7 not in other browser please refer these aritcles by Douglas Crockford and IBM Devloper Site

serioys sam
does firefox have such leak?
jscoot
no firefox doesnt have such leak only ie has this.
serioys sam
+1  A: 

IE is leak prone. You can test your code for leaks in IE with Microsofts javascript memory leak detector.

Firefox can leak too. This article explains why. There is also a leak detector plugin for Firefox (haven't tested it yet).

KooiInc
Tested the Firefox leak detector plugin: stackoverflow shows a lot of leaks!
KooiInc
seems like our browser engine needs a revamp in Web 2.0 and for our future:-)
jscoot