【发布时间】:2011-05-10 00:54:13
【问题描述】:
我有一个很奇怪的。我有一个工作得很好的方法,但我正在尝试将其转换为使数据更多地由数据库驱动,而不是当前存在的硬编码。当我添加对我的数据库方法的调用以提取数据时,在数据库完成后很长时间我得到了一个 EXC_BAD_ACCESS。我已将其范围缩小到数据库内容,因为如果我根本不进行调用,就不会发生崩溃。
因此,对该方法的调用如下所示(如果我注释掉,它不会崩溃):
NSString *thisRoomNumber = [self readLocationsFromDatabaseBetweenPoints:fX YPoint:fY];
方法本身如下所示:
#pragma mark -
#pragma mark SQL Operations
- (NSString *)readLocationsFromDatabaseBetweenPoints:(float)tapPointX YPoint:(float)tapPointY{
// Setup some globals
databaseName = @"db.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Setup the database object
sqlite3 *database;
NSString *sqlStringBuilder = [NSString stringWithFormat:@"select * from floors where propertyid = ? and floorid = ? and %f between topleftxcoord and bottomrightxcoord and %f between topleftycoord and bottomrightycoord", tapPointX, tapPointY];
NSString *roomNumber = @"0";
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = [sqlStringBuilder UTF8String];
NSLog(@"%@", [NSString stringWithUTF8String:sqlStatement]);
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiledStatement, 1, iPropertyId);
sqlite3_bind_int(compiledStatement, 2, iFloorId);
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSInteger aLocId = sqlite3_column_int(compiledStatement, 0);
NSInteger aTLX = sqlite3_column_int(compiledStatement, 1);
NSInteger aTLY = sqlite3_column_int(compiledStatement, 2);
NSInteger aBRX = sqlite3_column_int(compiledStatement, 3);
NSInteger aBRY = sqlite3_column_int(compiledStatement, 4);
NSString *aRoomIdent = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
roomNumber = aRoomIdent;
}
}else{
NSLog(@"%d", sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL));
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
[documentsDir release];
[documentPaths release];
//[roomNumber release];
[sqlStringBuilder release];
return roomNumber;
}
调用本身运行良好。我得到了预期的结果,“thisRoomNumber”返回了我期望的结果。一旦调用方法跳到下一部分,它就会返回到委托,此时它会崩溃。我知道如果不使用大量代码,很难看到发生了什么,但绝对数量的代码会让这篇文章变得非常庞大。
希望这将为我们提供解决此问题的起点。此外,崩溃日志转储对此毫无用处。
感谢你们提供的任何帮助!如果您需要更多信息,请告诉我,我会根据需要尽力添加。
【问题讨论】:
-
您可能不小心减少了不应该引用的引用计数:“构建和分析”是否给出任何警告?
-
我没有得到任何真正的警告(除了一些我知道并且不会破坏任何东西的东西 - 一些未使用的值等,但那些一直存在,我只是没有圈出回来完成那部分)
标签: objective-c cocoa-touch sqlite