Reputation: 541
I got this problem in developing my first iOS app that manage a DB. I have a DB with scores of the game and want to show them in a table, when (at runtime) I enter the scoreView app crash with this error:
Thread 1:EXC_BAD_ACCESS(code=1,address=0x726f635f)
Here is the code that Xcode show me as responsible of this problem:
if (sqlite3_prepare_v2(database, sql , -1, &select_statement, NULL)
== SQLITE_OK) {
while (sqlite3_step(select_statement) == SQLITE_ROW){
// read from DB
NSString *rank = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_statement, 0)];
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_statement, 1)];
NSString *score = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_statement, 2)];
NSString *date = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_statement, 3)];
// I insert all the string in a dictionary
NSDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:rank,@"Rank",name,@"Name",score,"Score",date,@"Date",nil];
[tempList addObject:dictionary];
}
self.list = tempList;
sqlite3_finalize(select_statement);
}
The error is in the line:
NSDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:rank,@"Rank",name,@"Name",score,"Score",date,@"Date",nil];
After reading from DB I'm trying to save all strings in a dictionary and at the end all dictionaries (in tempList) go into "list", an array to show scores in the table. I read on the internet that this problem may be caused by different threads accessing the same object but I don't know how to solve it. I was following a tutorial for use DB in iOS app and of course this problem wasn't there. If someone can help me I'll very appreciate, thank you.
Upvotes: 0
Views: 2046
Reputation: 4272
So first of all, I didn't see the line where you initialized your mutable array tempList before you trying to add an object.
NSMutableArray* tempList = [[NSMutableArray alloc]init];
And you have syntax error, and NSDictionary
can't be initialized as NSMutableDictionary
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:rank,@"Rank",name,@"Name",score,@"Score",date,@"Date",nil];
[tempList addObject:dictionary];
And for all string:
NSString *score = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(select_statement, 2)]
Upvotes: 0
Reputation: 1739
You have a syntax error:
... score, "Score" ...
should read:
... score, @"Score" ...
Upvotes: 1