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

Ios 如何解决以下内存泄漏问题?,ios,appcelerator,Ios,Appcelerator,如何解决以下内存泄漏问题? 我在解决iOS应用程序Ti SDK 6.2中的内存泄漏问题时遇到了麻烦,但没有取得多大成功 打开和关闭窗口时,xcode Instruments分配工具会显示内存中保留的大量TiUIxxxxProxy对象 为了测试/调试这个问题,我创建了一个超级简单的经典appcelerator应用程序(代码如下所示)。使用下面的代码,我从app.js打开window1,然后从window1上的按钮打开window2 您可以在附加的xcode图像中看到,在window2关闭后,代理对

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

我在解决iOS应用程序Ti SDK 6.2中的内存泄漏问题时遇到了麻烦,但没有取得多大成功

打开和关闭窗口时,xcode Instruments分配工具会显示内存中保留的大量TiUIxxxxProxy对象

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

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

App.js

require('Window1').CreateWindow().open();
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);
    }                   
};
Window1.js

require('Window1').CreateWindow().open();
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);
    }                   
};


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

  • 从行中删除标签
  • 从节中删除该行
  • 从表中删除该部分
  • 从窗口移开桌子
  • 从窗口中卸下按钮
然后

  • 使标签、行、节、表和按钮为空
最重要的是:

  • 清除指向节/行数组的指针
完成后,请确保附近有RemoveEventListener,以便也删除事件处理程序

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

最后一件事,如果以后不需要,请不要在window1中创建window2指针,因此:

require("window2").createWindow().open();
优于:

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

这样我就可以打开window2 8多次,之后一切都清理干净了


当窗口关闭时,尝试将用于声明window2的变量设置为“未定义”,看看这是否会清除对象。遗憾的是,没有更改。win2=未定义;设置win2=null;然后尝试一下你不使用Alloy的原因?你是否提供用Alloy重建应用程序?如果你使用Alloy,你会帮自己一个忙,造成的问题也会少一些,这对你来说即使不是全部,也会起到很大的作用。