-1

当我试图插入数据到我的表,有时我得到这个错误,我的应用程序崩溃!SQLite错误:EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,子代码= 0x0)-iOS

请先解决我的问题。

enter image description here

崩溃日志:

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\")" 
+0

你在做后台线程任何DB操作? – KrishnaCA

+0

雅,我取在背景数据,,,,和下此块在数据库 存储“dispatch_async(dispatch_get_main_queue(),^ { });” – Dhiru

+0

是否有任何数据库操作在并行线程中去。我认为这可能是由于在不同的线程同时访问数据库 – KrishnaCA

回答

0

这类问题的解决方案,你可以在下面失踪声明。

sqlite3_finalize(statement); 
     sqlite3_close(database); 

sqlite3_open() 
sqlite3_prepare_v2() 

后,我们应该始终敲定语句和return语句之前关闭数据库。不要离开数据库。
没有敲定声明,但不关闭数据库,如果你试图再次打开它sqlite3_open()

sqlite3_prepare_v2() 

这将导致EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=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; 

} 
0

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

@synchronized (self) { 
    // do the operation 
} 

请让我知道它是否有效或无效。随意建议编辑。

1
  • 尝试在malloc_error_break上设置断点。

  • 将您的变量设置为零后释放它们。

  • 仔细查看所有对sqlite3_prepare_v2指令的调用,并确保为它们中的每一个调用匹配的sqlite3_finalize

  • 添加@synchroinized块,使其线程安全

相关问题