您如何解决以下内存泄漏问题?

时间:2017-09-27 22:16:00

标签: ios appcelerator

您如何解决以下内存泄漏?

我无法在iOS应用Ti SDK 6.2中发现内存泄漏,但没有取得多大成功。

当打开和关闭窗口时,xcode Instruments Allocations工具会显示许多TiUIxxxxProxy对象保留在内存中。

为了测试/调试问题,我创建了一个超级简单的经典appcelerator应用程序(代码如下所示)。使用下面的代码我从app.js打开window1,然后从window1上的按钮打开window2。

您可以在附加的xcode Instruments Allocations图像中看到,在关闭window2之后,代理对象仍然存在(窗口,表格等)。更糟糕的是,多次打开和关闭window2会不断添加使用内存的其他代理对象。

App.js

require('Window1').CreateWindow().open();

Window1.js

exports.CreateWindow = function(){
    try{
        var window1 = Ti.UI.createWindow({
            title:'Window 1',backgroundColor:'yellow',
        });

        var button1 = Ti.UI.createButton({
            top:'50dp', center:'50%',
            borderWidth:'1dp',borderColor:'black',
            title:' Open Window 2  '            
        });
        window1.add(button1);

        button1.addEventListener('click', function() { 
          var win2 = require('Window2').CreateWindow();
          win2.open();
        });

        return window1;
    }
    catch(e){
        alert('Window 1 Error: ' + e);
    }                   
};

Window2.js

exports.CreateWindow = function(){
    try{
        var window2 = Ti.UI.createWindow({
                            title:'Window 2',layout:'vertical'
                            ,top:'200dp',bottom:'200dp',width:'80%'
                            ,backgroundColor:'orange'
         });

        var button2 = Ti.UI.createButton({
            top:'50dp', center:'50%',
            borderWidth:'1dp',borderColor:'black',
            title:' Close Window 2  '    
        });
        window2.add(button2);

        button2.addEventListener('click', function() { 
            window2.close();                                                  
        });

        // create a table row
        var tvRow1   = Ti.UI.createTableViewRow({ });

        //create a label to display location name
        var labelRow1 = Ti.UI.createLabel({
              color:'#000000',
              left:'15dp',
              top:'10dp',
              text:'Label in row 1'
            });                         
        tvRow1.add(labelRow1);

        // define table section for this group of rows
        var tableViewSection1 = Ti.UI.createTableViewSection({ });
        // push row into table view section 
        tableViewSection1.add(tvRow1);

        //create array to store table sections
        var arrayTableSections = [];

        //add table section to array
        arrayTableSections.push(tableViewSection1);        

        // create table 
        var table2 = Titanium.UI.createTableView({
                    data: arrayTableSections,
                    top:'50dp',
                    left:'50dp',
                    right:'50dp',
                    height:Ti.UI.SIZE,
                    backgroundColor:'#ffffff' ,
                    borderColor:'black',borderWidth:'1dp'
        });

        // add table to window
        window2.add(table2);         

        return window2;
    }
    catch(e){
        alert('Window 2 Error: ' + e);
    }                   
};

enter image description here

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

您遇到的具体问题是您手动创建元素而不是清理。因此,在Window2结束时,您应该从父项中删除所有元素,最终从Window2中删除:

  • 从行中删除标签
  • 部分删除该行
  • 从表中删除该部分
  • 从窗口中删除表格
  • 从窗口中删除按钮

然后

  • 使标签,行,部分,表格和按钮
  • 无效

最重要的是:

  • 清除指向部分/行数组的指针。

完成后,请确保您已关闭removeEvenlistener以删除事件处理程序。

最后,关闭窗口并使其无效。

最后一点,如果你以后不需要它,请不要在window1中创建一个window2指针:

require("window2").createWindow().open();

优于:

var window2 = require("window2").createWindow();

window2.open();

这样做我能够打开window2 8次以上,之后一切都被清理干净了。

enter image description here