问题描述
这让我头疼了几个星期了 - 希望有人可以提供帮助。我正在使用Titanium mobile中的2.1.4 SDK为iOs 5.0构建应用程序。我的应用程序从数据库中提取数据,并使用标签和表格在可滚动视图中显示它。我注意到它会在一段时间后崩溃,根据乐器,scrolllableView中的视图以及标签和tableview永远不会从内存中释放出来。我使用了很多commonJS,我的代码看起来像这样(剥离):
this has been giving me a headache for a couple weeks now -- hope somebody can help. I'm building an app for iOs 5.0, using the 2.1.4 SDK in Titanium mobile. My app pulls data from a database, and displays it in a scrollableview using labels and a table. I've noticed that it crashes after a while, and according to instruments, the views within the scrollableView, along with the labels and tableviews, are never released from memory. I use a lot of commonJS, and my code looks someting like this (stripped down):
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有了它,但我为了简洁起见删除了它。即便没有,也没有任何东西被释放。我真的很茫然,任何建议都表示赞赏。
I originally had some code in there to pull data for the answers from the database, and fill the tableView with that, but I removed it for the sake of brevity. Even without that, nothing gets released. I'm really at a loss here, any advice is appreciated.
推荐答案
最后,我解决了这个问题。就是这样:
In the end, I solved it. It was this:
var answView = Ti.UI.createTableView({
(...)
_parent: qview
});
引用答案表中的视图,使其及其所有子项免于被释放。一旦我删除它,只需关闭窗口即可释放它们。希望这对某人有用。
referencing the view in the answer table kept it, and all its children, from being released. Once I removed that, they get released simply by closing the window. Hope that's useful for someone.
这篇关于Titanium选项卡式iPhone应用程序中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!