首页 > 解决方案 > sqlite3_step 在 iOS 12 中导致 EXC_BAD_INSTRUCTION 崩溃

问题描述

我继承了一些导致以下崩溃的代码,当我在应用程序中多次执行完全相同的操作时,大约 20 次调用该方法:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

这是在线上发生的int resultCode = sqlite3_step(compiledStatement)

我找不到任何其他有同样错误的帖子。谁能帮我弄清楚为什么会发生这种崩溃,以及为什么它不会一直发生?这似乎只在 iOS 12 上崩溃,我无法在 iOS 14.5 上的设备上重新创建崩溃

- (void) execSql:(NSString *) theSql
     sqlArgs:(NSArray *) theArgs {

sqlite3_stmt *compiledStatment = [self _execRawSql:theSql
                                           sqlArgs:theArgs];

int resultCode = sqlite3_step(compiledStatment);
if (resultCode == SQLITE_DONE) {
    int finalizeResultCode = sqlite3_finalize(compiledStatment);
    
    if (finalizeResultCode != SQLITE_OK) {
        NSMutableString *errorReason = [NSMutableString string];
        [errorReason appendFormat:@"result code: \"%d\".\n",finalizeResultCode];
        [errorReason appendFormat:@"sqlite3_errmsg : \"%s\".\n",sqlite3_errmsg(myDatabase)];
        
        NSException* exception = [NSException
                                  exceptionWithName:@"SQLITE sqlite3_finalize EXCEPTION"
                                  reason:errorReason
                                  userInfo:nil];
        @throw exception;
    }

    return;
}

NSMutableString *errorReason = [NSMutableString string];
[errorReason appendFormat:@"result code: \"%d\".\n",resultCode];
[errorReason appendFormat:@"sqlite3_errmsg : \"%s\".\n",sqlite3_errmsg(myDatabase)];

NSException* exception = [NSException
                          exceptionWithName:@"SQLITE sqlite3_step EXCEPTION"
                          reason:errorReason
                          userInfo:nil];
@throw exception;
}

如果它是相关的,这是execRawSql在 sqlite3_step 之前调用的前面的方法:

- (sqlite3_stmt *) _execRawSql:(NSString *) theSql
                   sqlArgs:(NSArray *) theArgs {


sqlite3_stmt *compiledStatement;
int resultCode = sqlite3_prepare_v2(myDatabase,
                                    [theSql UTF8String],-1,
                                    &compiledStatement,
                                    NULL);

if (resultCode == SQLITE_OK) {
    if (theArgs!=NULL) {
        for (int i = 0; i < [theArgs count]; i++ ) {
            [self _bindObject:theArgs[i]
                     toColumn:i+1
                  inStatement:compiledStatement];
        }

    }
}
else {

    NSMutableString *errorReason = [NSMutableString string];
    [errorReason appendFormat:@"result code: \"%d\".\n",resultCode];
    [errorReason appendFormat:@"sqlite3_errmsg : \"%s\".\n",sqlite3_errmsg(myDatabase)];

    NSException* exception = [NSException
                                exceptionWithName:@"SQLITE sqlite3_prepare_v2 EXCEPTION"
                                reason:errorReason
                                userInfo:nil];
    @throw exception;

}

      
return compiledStatement;
}

编辑:在 iOS 14 中发生崩溃,在上面的 execRawSql 方法中抛出异常sqlite3_errmsg : "no such function: COUNT".

标签: iosobjective-csqlite

解决方案


推荐阅读