Skip to content

Instantly share code, notes, and snippets.

@DrewMartin
Created March 8, 2015 19:06
Show Gist options
  • Select an option

  • Save DrewMartin/2ec774e088fdb26a425b to your computer and use it in GitHub Desktop.

Select an option

Save DrewMartin/2ec774e088fdb26a425b to your computer and use it in GitHub Desktop.
jQuery memory leaking
(function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
window.Leak = (function() {
function Leak(node, data) {
this.data = data;
this.onNodeChanged = __bind(this.onNodeChanged, this);
$(node).on("change", this.onNodeChanged);
}
Leak.prototype.onNodeChanged = function() {
return console.log("node changed with data", this.data);
};
return Leak;
})();
}).call(this);
node = $('<div id="will-leak"></div>')[0];
document.body.appendChild(node);
new Leak(node, 'gibberish');
$(node).trigger('change');
node2 = $('<span id="replace-leak"></span>')[0];
document.body.replaceChild(node2, node);
// $(node).remove()
node = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment