SQLite错误:EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0)-iOS

SQLite错误:EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0)-iOS,ios,objective-c,multithreading,sqlite,grand-central-dispatch,Ios,Objective C,Multithreading,Sqlite,Grand Central Dispatch,当我试图将数据插入表时,有时会出现此错误,我的应用程序会崩溃 崩溃日志: Observation(4001,0x10dd67000) malloc: *** error for object 0x7fff3a917100: Non-aligned pointer being freed (2) *** set a breakpoint in malloc_error_break to debug 2016-11-03 11:12:03.063 Observation[4001:46477] I

当我试图将数据插入表时,有时会出现此错误,我的应用程序会崩溃

崩溃日志:

Observation(4001,0x10dd67000) malloc: *** error for object 0x7fff3a917100: Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug
2016-11-03 11:12:03.063 Observation[4001:46477] Insertion failed !


Printing description of dbpath:
(const char *) dbpath = 0x00007fff3b8a5690 "/Users/macbt/Library/Developer/CoreSimulator/Devices/0EEC62AE-6DF0-4FC4-9D30-1EB90CB695A5/data/Containers/Data/Application/A709E729-3162-4CC8-B9FF-2F22A32FC6BD/Documents/ObservationDB.db"


Printing description of insertSQL:
insert into table_hazard (id, name, modifiedDate) values ("1","Hazard", "03/11/2016 11:12:03 AM")


Printing description of insert_stmt:
(const char *) insert_stmt = 0x00007fff3b9291a1 "insert into table_hazard (id, name, modifiedDate) values (\"1\",\"Hazard\", \"03/11/2016 11:12:03 AM\")"

我相信问题可能是由于同时访问不同线程中的数据库造成的。您可以使用线程锁来解决此问题。您可以在
@synchronized
块中编写数据库操作代码来解决此问题。它可以实现如下

@synchronized (self) {
    // do the operation
}
请让我知道它是有效的还是无效的。请随意提出编辑建议。

  • 尝试在
    malloc\u error\u break
    上设置断点

  • 释放变量后,将其设置为零

  • 仔细检查对sqlite3\u prepare\u v2指令的所有调用,确保为每个指令调用匹配的
    sqlite3\u finalize

  • 添加
    @synchronized
    块以使其线程安全


使用Fmdb for sqlite实现这很容易理解

这类问题的解决方案是,您可能缺少下面的语句

 sqlite3_finalize(statement);
        sqlite3_close(database);
以后

sqlite3_open()
sqlite3_prepare_v2()
我们应该总是在返回语句之前完成语句并关闭数据库。不要让数据库保持打开状态。
如果您尝试再次打开数据库,则不会终止语句,也不会关闭数据库
sqlite3\u open()

这将导致有关数据库的
EXC\u BAD\u指令(code=EXC\u I386\u INVOP,子代码=0x0)

例如:

-(BOOL) insertDropdownValues:(NSString *)tableName
                       andId:(NSInteger) dID
                        name:(NSString*) name
                modifiedDate:( NSString*) modifiedDate {

    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {

        NSString *insertSQL = [NSString stringWithFormat:@"insert into %@ (%@, %@, %@) values (\"%ld\",\"%@\", \"%@\")",tableName,ID,NAME,MODIFIED_DATE, dID,name,modifiedDate];

        const char *insert_stmt = [insertSQL UTF8String];


        sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);


        if (sqlite3_step(statement) == SQLITE_DONE)
        {

            NSLog(@"Inserted SuccessFull");
            sqlite3_finalize(statement);
            sqlite3_close(database);

            return YES;
        }
        else {

            NSLog(@"Insertion failed !");
            sqlite3_finalize(statement);
            sqlite3_close(database);

            return NO;
        }

    }

    sqlite3_reset(statement);
    return NO;

}

您是否在后台线程中执行任何db操作?是的,我正在后台、、、中获取数据,并将其存储在数据库中的“dispatch_async(dispatch_get_main_queue(),^{})”块下是否有任何数据库操作在并行线程中进行。我想可能是因为同时访问不同线程中的数据库,我想是的,但是如何解决这个问题呢?您可以使用
@synchronized
块来解决这个问题。无论您在哪里访问db,都要在
@synchonized
块中写入db操作的代码。这可能会解决问题。请让我知道,如果你需要像实施等其他细节。
-(BOOL) insertDropdownValues:(NSString *)tableName
                       andId:(NSInteger) dID
                        name:(NSString*) name
                modifiedDate:( NSString*) modifiedDate {

    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {

        NSString *insertSQL = [NSString stringWithFormat:@"insert into %@ (%@, %@, %@) values (\"%ld\",\"%@\", \"%@\")",tableName,ID,NAME,MODIFIED_DATE, dID,name,modifiedDate];

        const char *insert_stmt = [insertSQL UTF8String];


        sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);


        if (sqlite3_step(statement) == SQLITE_DONE)
        {

            NSLog(@"Inserted SuccessFull");
            sqlite3_finalize(statement);
            sqlite3_close(database);

            return YES;
        }
        else {

            NSLog(@"Insertion failed !");
            sqlite3_finalize(statement);
            sqlite3_close(database);

            return NO;
        }

    }

    sqlite3_reset(statement);
    return NO;

}