Yas
Yas

Reputation: 71

iOS App crashes on sqlite3_reset with EXC_BAD_INSTRUCTION

My iOS app terminates in Xcode simulator due to EXC_BAD_INSTRUCTION (code=EXC_i386_INVOP) error on following line

sqlite3_reset(stmt);

The complete block of code is

-(BOOL)containsPendingSignature
{
sqlite3_stmt *stmt = nil;

const char* sql = "SELECT Count(*) FROM pending_signatures";
if(sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error:'%s'", sqlite3_errmsg(database));


int rowCount = 0;
if(sqlite3_step(stmt) == SQLITE_ROW) {
    rowCount = sqlite3_column_int(stmt, 0);
}

sqlite3_reset(stmt);
sqlite3_finalize(stmt);
return rowCount > 0;
}

Could any one please suggest what is wrong here, thank you in advance.

Upvotes: 1

Views: 1293

Answers (1)

Dhiru
Dhiru

Reputation: 3060

The solution of this type of problems is , you may be missing below statement.

 sqlite3_finalize(statement);
        sqlite3_close(database);

after every

sqlite3_open()
sqlite3_prepare_v2()

we should always finalize the statement and close the database before return statement. Don't leave database open. without finalizing statement and without closing Database if you try to again open it sqlite3_open()

or

sqlite3_prepare_v2()

this will result in EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) regarding database .

 -(BOOL)containsPendingSignature
    {
    sqlite3_stmt *stmt = nil;

    const char* sql = "SELECT Count(*) FROM pending_signatures";
    if(sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) != SQLITE_OK)
    NSAssert1(0, @"Error:'%s'", sqlite3_errmsg(database));


    int rowCount = 0;
    if(sqlite3_step(stmt) == SQLITE_ROW) {
        rowCount = sqlite3_column_int(stmt, 0);
    }

     sqlite3_finalize(statement);
     sqlite3_close(database);
    return rowCount > 0;
    }

i hope this will help you ....

Upvotes: 2

Related Questions