代码之家  ›  专栏  ›  技术社区  ›  Newport99

如何解决以下内存泄漏?

  •  0
  • Newport99  · 技术社区  · 7 年前

    我正在解决iOS应用程序Ti SDK 6.2中的内存泄漏问题,但没有取得多大成功。

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

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

    应用程序。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 回复  |  直到 7 年前
        1
  •  1
  •   jasonkneen    7 年前

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

    然后

    完成后,请确保在附近有一个RemoveEventListener来删除事件处理程序。

    最后,关闭窗口并将其置零。

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

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

    优于:

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

    window2.open();

    enter image description here