Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 8 years ago

#1242 closed bug (fixed)

Unload event handlers may cause memory leaks in Internet Explorer

Reported by: choan Owned by:
Priority: major Milestone: 1.1.3
Component: event Version: 1.1.2
Keywords: internet explorer, leaks Cc:
Blocked by: Blocking:

Description

As the unload event handlers are not removed by the event system, they may cause memory leaks in Internet Explorer.

  • This leaks seams to appears only when a jQuery page is openened in a new window which is then closed.
  • The leak doesn't appear if the event handler unregisters itself.
  • It'd be nice to have the fix in core (Patch attached).

Steps to reproduce:

Create a page with the following code:

$(window).unload(function(){});

Navigate to this page with Internet Explorer 6. Open some new windows, watch the memory in use grow. Close all windows except the first one. Repeat some times. (Memory usage graphic attached.)

Fixes:

If we leave the work to be done by the developer:

// Use `one` instead of `bind` for unload event handlers
// the handler will be autounregistered
$(window).one("unload", function(){});

// Unbind the handler by itself
$(window).bind("unload", function(e) {
  $(this).unbind(e);
});

// In pre 1.1 versions
$(window).bind("unload", function(e) {
  $(this).unbind("unload", arguments.callee);
});

With the proposed fix, every bind("unload", f) would delegate in one("unload", f) so the programmer won't have to think about this leak.

Attachments (3)

unfixed-no-closure.png (10.6 KB) - added by choan 13 years ago.
Memory grow without the fix
fixed-no-closure.png (11.5 KB) - added by choan 13 years ago.
Memory usage doesn't grow
patch-unload2one.txt (433 bytes) - added by choan 13 years ago.
Proposed patch

Download all attachments as: .zip

Change History (4)

Changed 13 years ago by choan

Attachment: unfixed-no-closure.png added

Memory grow without the fix

Changed 13 years ago by choan

Attachment: fixed-no-closure.png added

Memory usage doesn't grow

Changed 13 years ago by choan

Attachment: patch-unload2one.txt added

Proposed patch

comment:1 Changed 13 years ago by brandon

Resolution: fixed
Status: newclosed

Thanks choan! This is a great way to help avoid those pesky memory leaks. This patch is now applied in Rev [2010].

Note: See TracTickets for help on using tickets.