2016-09-28 58 views
0

我有一些關於內存泄漏的問題。 請看看下面的代碼塊:jquery,劍道內存泄漏問題

(function ($) { 

$.fn.bnftWindow = function (options) { 

    // Default Settings 
    var settings = $.extend({ 
    }, options); 

    // Validate parameters 
    if (settings.id === undefined || settings.id === null || settings.id === "") { 
     throw "bnftWindow id option is undefined, null or empty string"; 
    } 


    // If window container div already exists 
    if ($("#" + settings.id).length) { 
     alert("bnftWindow id already exists - will be destroyed and recreated"); 
     // destroy window container div 
     $("#" + settings.id).remove(); 
    } 

    // create window container div 
    $("body").append("<div id='" + settings.id + "'></div>"); 

    var currentInstance = $("#" + settings.id); 

    return currentInstance.each(function() { 

     currentInstance.data("bnftWindow", currentInstance); 

     var widgetId = currentInstance[0].id; //The id of the html element used to create the bnft widget 

     // Apply settings 
     $('#' + widgetId).kendoWindow(
      settings 
     ); 

     var dialog = $('#' + widgetId).data("kendoWindow"); 

     currentInstance.destroy = function() { 


      kendo.destroy($("#" + widgetId)); 

      $("#" + settings.id).remove(); 
     } 
    }); 



    function onRefresh(e) { 

     // Always center window first 
     if (settings.refresh !== undefined) { 
      settings.refresh(e); 
     } 

     var dialog = $("#" + settings.id).data("kendoWindow"); 
     dialog.center(); 
    } 
}; 

}(jQuery)); 

和代碼塊:

function Test() 
{ 
    var element = $("#myElement"); 
    element.hide(); 
    var that = this; 

    this.ids = { 
     grid: "messagesGrid", 
     gridButton: "gridButton", 
     composeWnd: "composeWnd" 
    }; 

    this.grid = null; // will store the grid later 


     $("#" + this.ids.gridButton).on("click", function() { 
      try { 
        // Do something.... 
        that.init(); 
       } 
      } 
      catch (ex) { 
       alert("Error: " + ex); 
      } 
     }); 

} 

Test.prototype.init = function() 
{ 
    // Do something else... 
    var that = this; 

    setTimeout(function() { 
     that.createWidget(); 
    }, 500); 
} 

Test.prototype.createWidget = function() 
{ 
    // Do something else... 
    $("#grid").kendoGrid({ // Some properties here }); 

    // store grid 
    this.grid = $("#grid").data("kendoGrid"); 
    // Blah blah blah 
} 
  1. 執行變量currentInstance,控件ID,對話框,元素,如此,這款。網格或事件處理程序導致內存泄漏?

  2. 如果元素變量導致內存泄漏,則爲$(「#myElement」)。hide();解決方案? (除了在隱藏之後將元素變量設置爲空)。

在此先感謝您。

+0

你嘗試做一個時間軸上使用Chrome開發者工具記錄?這將幫助您輕鬆查看是否有泄漏。您可以查看[此視頻](https://youtu.be/LaxbdIyBkL0?t=236)查看跟蹤內存泄漏的工作流示例。 – Corina

回答

0

以下爲我工作,清理對話實例:

_create: function() {   
     var that = this; 
     ...  
     that.frmElement = that.wrapper.kendoWindow({ 
      ... 
      close: function (e) { 
       this.destroy(); 
       that.wrapper.remove("#" + that.options.dialogId); 
       that.wrapper.empty(); 
      } 
      ... 
     }).data("kendoWindow"); 
    }