2012-12-20 45 views
2

這讓我頭疼幾個星期 - 希望有人能夠提供幫助。我正在爲iOs 5.0構建一個應用程序,使用Titanium mobile中的2.1.4 SDK。我的應用程序從數據庫中提取數據,並使用標籤和表格將其顯示在可滾動視圖中。我注意到它在一段時間後崩潰了,並且根據樂器,scrollableView中的視圖以及標籤和tableviews絕不會從內存中釋放。我用了很多CommonJS的,而我的代碼看起來像這樣成才(精簡):Titanium標籤式iPhone應用程序中的內存泄漏

的ApplicationWindow模塊,這是我從app.js

function ApplicationWindow(title) { 
var self = Ti.UI.createWindow({ 
    title:title, 
    backgroundColor:'white' 
}); 

var button = Ti.UI.createButton({ 
    height:44, 
    width:200, 
    title:L('openWindow'), 
    top:20 
}); 
self.add(button); 

var QuizWindow = require('/ui/common/QuizWindow'); 

button.addEventListener('click', function() { 
    //containingTab attribute must be set by parent tab group on 
    //the window for this work 
    qw = new QuizWindow({title: 'KCT', backgroundColor: 'white', navBarHidden: true, tabBarHidden: true}); 
    self.containingTab.open(qw); 
}); 

return self; 
}; 

module.exports = ApplicationWindow; 

然後叫我打電話QuizWindow模塊:

function QuizWindow(args){ 
var instance = Ti.UI.createWindow(args); 
var QuestionView = require('/ui/common/QuestionView'); 

var db = Ti.Database.install("/kct.sqlite","kct"); 
var q = db.execute("SELECT * FROM questions"); 

var sv = Ti.UI.createScrollableView({ 
    bottom: 40 
}); 

while(q.isValidRow()){ 
    var id = q.fieldByName("qid"); 
    var question = q.fieldByName("q"); 
    var set = q.fieldByName("set"); 
    var info = q.fieldByName("info"); 

    var v = new QuestionView(id,set,question,info); 
    sv.addView(v); 
    q.next(); 
} 

q.close(); 
db.close(); 

var button = Ti.UI.createButton({ 
    height:44, 
    width:200, 
    title:"Close", 
    bottom:10 
}); 

button.addEventListener('click', function() { 
    instance.close({animation: true}); 
}); 

instance.add(sv); 
instance.add(button); 
return instance; 
}; 
module.exports = QuizWindow; 

,這是QuestionView:

function QuestionView (id,set,question,infolert) { 
var qview = Ti.UI.createView({ 
    top: 0, 
    width: 'auto', 
    height: 'auto', 
    backgroundColor: 'white', 
    questionSet: set, 
    questionID: id, 
    info: infolert, 
    isAnswered: false  
}); 

var qLabel = gui.Header(question); 
qLabel.top = 5; 
qLabel.left = 10; 
qLabel.right = 10; 
qLabel.color = "#3B3B3B"; 
qLabel.font = {fontSize: gui.defaultFontSize+4, fontWeight: 'bold'}; 

var dcalc = (qLabel.text.length/30) * ((gui.defaultFontSize+4)*1.2) + 5; 

var answView = Ti.UI.createTableView({ 
    backgroundColor: 'white', 
    top: dcalc, 
    _parent: qview 
}); 

var changeheight = function(e) { 
    var lheight = e.source.rect.height; 
    if(lheight && lheight > 10) answView.top = lheight + 5; 
    return; 
}; 

qLabel.addEventListener('postlayout', changeheight); 


qview.add(qLabel); 
qview.add(answView); 

return qview; 

} 
module.exports = QuestionView; 

我最初有一些代碼在那裏爲數據庫中的答案提取數據,並填充tableView,但爲了簡潔起見,我刪除了它。即使沒有這個,也沒有任何東西被釋放我在這裏真的很茫然,任何意見都表示讚賞。

回答

1

最後,我解決了它。它是這樣的:

var answView = Ti.UI.createTableView({ 
    (...) 
    _parent: qview 
}); 

引用答案表中的視圖保持它及其所有孩子不被釋放。一旦我將其刪除後,只需關閉窗口即可發佈。希望對某人有用。

1

你可以看看我的要點。通過使用這個,你可以很好地解決內存問題。只需將該課程包含在您的項目中並像這樣使用它。

https://gist.github.com/3898340

var mem = new MemoryClean(); 
    mem.clean(tableView); 
    mem.clean(headerView); 
    mem.clean(footerView); 
    mem = null; 

這是我一直在使用的東西。

function openAppointments(e) { 
    var mem = new MemoryClean(); 
    mem.clean(headerView); 
    mem.clean(footerView); 
    mem = null; 
    win.close(); 
} 
+0

我試圖用構造函數調用mem,將每個視圖,標籤和表添加到它,並在關閉窗口時將其設置爲null,但這並不奏效。他們仍然在堆疊。也許我做錯了,你有沒有關於如何使用它的例子? –

+0

信息語句在控制檯中打印的是什麼。 Ti.API.warn(「這是我們的記憶」+ Ti.Platform.availableMemory);.我想在關閉窗口時,它應該被釋放。 – Thalaivar

+0

它一直給我一個錯誤,沒有釋放內存......也許這只是我。 –