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):
The ApplicationWindow module, which I call from 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;
I then call the QuizWindow module:
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;
and this is the 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;
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:
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.
You can have a look at my Gist. By using this you can solve out the memory issue to great extend. Just include the class in your project and use it like this.
https://gist.github.com/3898340
Here is what i have been using.