jQuery.ui.widget memory leak
HTML
<script src="https://raw.github.com/jquery/jquery-ui/master/ui/jquery.ui.core.js"></script>
<script src="https://raw.github.com/jquery/jquery-ui/master/ui/jquery.ui.widget.js"></script>
<div id="container">loading...</div>
<div id="console"></div>
JavaScript
(function($,undefined){
// jquery.ui.widget
$.Widget.prototype._createWidget = function( options, element ) {
element = $( element || this.defaultElement || this )[ 0 ];
this.element = $( element );
this.options = $.widget.extend( {},
this.options,
this._getCreateOptions(),
options );
this.bindings = $();
this.hoverable = $();
this.focusable = $();
if ( element !== this ) {
$.data( element, this.widgetName, this );
// <--- memory leak
//this._bind({ remove: "destroy" });
// <--- workaround -- start
this.element.bind( "remove." + this.widgetName,
{widgetName: this.widgetName},
this.__destroy);
// <--- workaround -- end
this.document = $( element.ownerDocument );
this.window = $( this.document[0].defaultView || this.document[0].parentWindow );
}
this._create();
this._trigger( "create" );
this._init();
};
// new __destroy method
$.Widget.prototype.__destroy = function(e){
var self = $(this).data(e.data.widgetName);
self.destroy();
}
})(jQuery);
// test code
(function($,undefined){
var count = 0;
$.widget("ui.dummy",{
_destroy: function(){
$("#console").text("call destroy:"+count++);
// $.Widget.prototype.destroy.call( this );
}
});
})(jQuery);
setInterval(function(){
$("#container")
.empty()
.append("<div class='dummy'>dummy</div>")
.find(".dummy")
.dummy()
.end();
},1000);