[TIMOB-5196] Reading from a database crashes the app
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Cannot Reproduce |
Resolution Date | 2011-09-13T15:09:50.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Sprint 2011-37 |
Components | iOS |
Labels | dr-list |
Reporter | João Silva |
Assignee | Sabil Rahim |
Created | 2011-08-23T04:29:26.000+0000 |
Updated | 2017-03-31T17:28:35.000+0000 |
Description
Starting from 1.7.0 up to the current 1.7.3.v20110808134952 CI build I'm getting random crashes in my app. I've managed to reproduce them, just copy the following code into the app.js of a new project:
function read_from_db(thisLabel)
{
Ti.API.info('read_from_db: start');
Ti.API.info('thisLabel' + thisLabel);
thisLabel.text = 'Reading from the database...';
var db = Ti.Database.open('data.db');
for(var i=0; i<10000; i++){
var resultSet = db.execute('select * from data');
if(resultSet.isValidRow()) {
Ti.API.info('Database row: (' + i + ') ' + resultSet.fieldByName('col1') + ' / '+ resultSet.fieldByName('col2'));
}
resultSet.close();
}
db.close();
thisLabel.text = 'Finished reading from the database.';
Ti.API.info('read_from_db: end');
}
var number_of_iterations = 5000; // This is not required
var number_of_db_rows = 200;
//Create and initialize the database
var db = Ti.Database.open('data.db');
db.execute('create table if not exists data (col1 primary key,col2)');
db.execute('begin transaction');
db.execute('delete from data');
for(var i=0; i<number_of_db_rows; i++) {
db.execute('insert or replace into data (col1,col2) values (?,?)',i,i+1);
}
db.execute('commit transaction');
db.close();
var tabGroup = Ti.UI.createTabGroup();
var win1 = Ti.UI.createWindow({
backgroundColor: 'green',
layout:'vertical'
});
var b = Ti.UI.createButton({
title:'Read from the DB',
top:60,
height:50
});
win1.add(b);
var label = Ti.UI.createLabel({
top:60,
height:60,
color:'white',
text:'Click the button to start'
});
win1.add(label);
var Tab1 = Titanium.UI.createTab({
title:'Tab 1',
window:win1
});
tabGroup.addTab(Tab1);
tabGroup.open();
b.addEventListener('click',function(e) {
read_from_db(label);
});
Clicking the button will crash the iPhone simulator.
Depending on the timing or the numbers chosen at the start sometimes this code will fail with a 'resultSet.isValidRow is not a valid function' instead of crashing.
The code presented in this issue clearly demonstrates an important bug in Titanium 1.7. Has anyone reproduced it yet? (See also the Q&A post http://developer.appcelerator.com/question/123736/reproducible-crash-and-errors-with-database-access-in-172)
I cannot get it to happen predictably enough to get a solid case. It happens every 10 - 20 times I run the simulator. It's a serious impediment to development. Once it happens, it will happen every time until I remove the app from the simulator.
Use my code in this updated ticket as a starting point to prove whether this behavior you have observed is actually a bug in Titanium. If it does, reopen this ticket. Some points to note: * use the
while
construct to iterate through your query * remember to declare variables using the javascriptvar
keyword * functions have scopes. You need to pass any data created outside of them as argumentsI've tested the latest code as requested in the Q&A. It crashes as described. See also http://developer.appcelerator.com/question/124936/app-very-unstable-using-17-fine-on-15 I'm reposting the reply here: I need to, for example, load a given record from a table from various items. So for each item of the list, I do a db.execute, check if the record exists, and close the resultSet. The real app doesn't crash all the time, this code is the reduced version to expose the problem. For me your latest code crashes exactly as I described above. {noformat} Ti version Simulator version Result ---------- ------------------ -------- 1.6.2 iOS 4.3 OK 1.7.0 iOS 4.3 Crash 1.7.2 iOS 4.0,4.2,4.3 Crash 1.7.3v20110826161351 iOS 4.3 Crash {noformat} What are the differences between your environment and mine? I'm using: Titanium Studio, build: 1.0.4.201108101535 Mac Mini (2.4GHz Intel Core 2 Duo, 4GB RAM) with Mac OS/X 10.6.8 Various versions of the iPhone simulator, as described above
Proved latest testcase is valid. Other users are reporting same issue.
Another user has provided some test code for this, in duplicate ticket TC-195. Please test with this also, to confirm fix: Copy this code into a newly created project
Run in IOS simulator Switch between tabs until crash occurs. In Studio the application will just exit, running under XCode gives a EXC_BAD_ACCESS with the following callstack
See [Q&A thread](http://developer.appcelerator.com/question/123951/crash-updating-window-from-focus-event-while-querying-database--ios-43--sdk-172)
I confirm that the code from ticket TC-195 posted above crashes in the same way, and with the same crash log as the code posted in this ticket.
Tested both codes on simulator 4.3 and iPhone 4 v 4.3.4 with Titanium SDK Build: 1.8.0.0c3b052 (09/13/11 14:44) and cannot replicate any crash. Please update with a better test case.
I don't understand, I can reproduce it every time. You can download my project from http://www.megaupload.com/?d=CORMUDY2
EDIT: It seems to be fixed in the CI build 1.8.0.v20110912131752. It does crash with the 1.7.x builds.
Closing ticket as I am unable to reproduce the issue using the following environment; iPhone 7 (10.2) MacOS 10.11.6 (15G31) Studio 4.8.1.201612050850 Ti SDK 6.0.3 GA Appc NPM 4.2.8 Appc CLI 6.1.0 Ti CLI 5.0.11 Alloy 1.9.5 Arrow 1.10.1 Xcode 8.2 (8C38) Node v4.6.0 Java 1.7.0_80