2

我得到这些让我发疯的 sqlite3 崩溃。也许这很明显我错过了?示例(来自 iOS 6 模拟器):

MyApp(14195,0xb0115000) malloc: *** error for object 0xffffffff: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
(lldb) bt
* thread #13: tid = 0x2d03, 0x94bd8875 libsystem_c.dylib`malloc_error_break, stop reason = breakpoint 2.1
frame #0: 0x94bd8875 libsystem_c.dylib`malloc_error_break
frame #1: 0x94be117a libsystem_c.dylib`szone_error + 270
frame #2: 0x94bdbc18 libsystem_c.dylib`szone_free + 232
frame #3: 0x02977221 libsqlite3.dylib`sqlite3MemFree + 33
frame #4: 0x029420df libsqlite3.dylib`sqlite3DbFree + 127
frame #5: 0x02980a7c libsqlite3.dylib`sqlite3SrcListDelete + 60
frame #6: 0x029b2ba1 libsqlite3.dylib`sqlite3FkCheck + 1425
frame #7: 0x029bde60 libsqlite3.dylib`sqlite3GenerateRowDelete + 608
frame #8: 0x02993cb1 libsqlite3.dylib`sqlite3DeleteFrom + 2177
frame #9: 0x02983786 libsqlite3.dylib`yy_reduce + 11030
frame #10: 0x02980535 libsqlite3.dylib`sqlite3Parser + 245
frame #11: 0x02947458 libsqlite3.dylib`sqlite3RunParser + 312
frame #12: 0x029c6137 libsqlite3.dylib`sqlite3Prepare + 695
frame #13: 0x0294664e libsqlite3.dylib`sqlite3LockAndPrepare + 270
frame #14: 0x02946735 libsqlite3.dylib`sqlite3_prepare_v2 + 53
frame #15: 0x002610f6 MyApp`-[MySqlDB prepare:](self=0x0b1abc90, _cmd=0x005694fc, sql=0x0057b1a5) + 294 at MySqlDB.m:123

我在 malloc_error_break 中添加了一个断点。现在,让我们进入 MySqlDB 的 prepare: 函数:

- (BOOL)prepare:(const char*)sql
{

    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK) {
    statement = nil;
    return NO;
    }
    return YES;
}

调用者函数是:

- (BOOL)deleteWithID:(NSInteger)theID {
    BOOL theResult;
const char *theQuery = "DELETE FROM addresses WHERE id = ?;";
    if ((theResult = [impl prepare:theQuery]))  // CALLER HERE
    {
    [impl setInt:theID forIndex:1];
    [impl step];
    }
[impl finalize_statement];


    return theResult;
}

有任何想法吗?如果我实际上在同一个数据库文件上运行 sqlite 并尝试使用健全的值(在上述情况下为 4)执行删除语句,则该语句执行得很好。

好的,我再次运行(并且还启用了 malloc 诊断、保护),这一次它设法在上面相同函数的 [impl step] 行上崩溃(它只是调用 sqlite3_step(statement)):

(lldb) bt
* thread #12: tid = 0x2b03, 0x029e493b libsqlite3.dylib`allocateCursor + 139, stop reason = EXC_BAD_ACCESS (code=1, address=0x715e6f40)
frame #0: 0x029e493b libsqlite3.dylib`allocateCursor + 139
frame #1: 0x029dd3d4 libsqlite3.dylib`sqlite3VdbeExec + 35684
frame #2: 0x0294fbc1 libsqlite3.dylib`sqlite3_step + 3169
frame #3: 0x00261398 MyApp`-[MySqlDB step](self=0x25409fe0, _cmd=0x043a9897) + 120 at MySqlDB.m:169
frame #4: 0x0018aeb0 MyApp`-[MyDataBase deleteWithID:]
4

1 回答 1

0

是的,好的,我想我用互斥锁解决了它。使用 @synchronized 进行 DELETE 后,我似乎无法重现该问题。但是,我在这些崩溃期间检查了其余线程,并且其他线程都没有做任何与数据库相关的事情,所以我不确定它是如何崩溃的。任何想法,尤其是。欢迎以后如何调试此类问题!

于 2013-07-23T01:47:19.353 回答