{"action":"created","issue":{"url":"https://api.github.com/repos/ccgus/fmdb/issues/521","repository_url":"https://api.github.com/repos/ccgus/fmdb","labels_url":"https://api.github.com/repos/ccgus/fmdb/issues/521/labels{/name}","comments_url":"https://api.github.com/repos/ccgus/fmdb/issues/521/comments","events_url":"https://api.github.com/repos/ccgus/fmdb/issues/521/events","html_url":"https://github.com/ccgus/fmdb/issues/521","id":171831551,"number":521,"title":"crash on [FMStatement reset] (FMDatabase.m:1470)","user":{"login":"Yuzeyang","id":12770548,"avatar_url":"https://avatars.githubusercontent.com/u/12770548?v=3","gravatar_id":"","url":"https://api.github.com/users/Yuzeyang","html_url":"https://github.com/Yuzeyang","followers_url":"https://api.github.com/users/Yuzeyang/followers","following_url":"https://api.github.com/users/Yuzeyang/following{/other_user}","gists_url":"https://api.github.com/users/Yuzeyang/gists{/gist_id}","starred_url":"https://api.github.com/users/Yuzeyang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Yuzeyang/subscriptions","organizations_url":"https://api.github.com/users/Yuzeyang/orgs","repos_url":"https://api.github.com/users/Yuzeyang/repos","events_url":"https://api.github.com/users/Yuzeyang/events{/privacy}","received_events_url":"https://api.github.com/users/Yuzeyang/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":10,"created_at":"2016-08-18T07:03:29Z","updated_at":"2016-08-25T14:14:54Z","closed_at":null,"body":"sometimes i insert message to database, it will crash on [FMStatement reset] (FMDatabase.m:1470)\r\n\r\nthis is crash log\r\n```\r\n0  libsystem_kernel.dylib         0x18379411c __pthread_kill + 8\r\n1  libsystem_pthread.dylib        0x183860ef8 pthread_kill + 112\r\n2  libsystem_c.dylib              0x183705dc8 abort + 140\r\n3  libsystem_malloc.dylib         0x1837c8d24 free_list_checksum_botch + 438\r\n4  libsystem_malloc.dylib         0x1837c8f28 free_small_botch + 84\r\n5  libsqlite3.dylib               0x1841254bc (null) + 7112\r\n6  libsqlite3.dylib               0x184124c10 (null) + 4892\r\n7  libsqlite3.dylib               0x1840e143c (null) + 876\r\n8  libsqlite3.dylib               0x1840e1248 (null) + 376\r\n9  libsqlite3.dylib               0x18411ff64 sqlite3_reset + 64\r\n10 xxx                     0x1003e778c -[FMStatement reset] (FMDatabase.m:1470)\r\n11 xxx                     0x1003ea424 -[FMResultSet close] (FMResultSet.m:55)\r\n12 xxx                     0x1003e3eb4 -[FMDatabase closeOpenResultSets] (FMDatabase.m:309)\r\n13 xxx                     0x1003e3b1c -[FMDatabase close] (FMDatabase.m:218)\r\n14 xxx                     0x1003e17dc __37-[FIMUserCRUD insertItem:inDatabase:]_block_invoke (FIMUserCRUD.m:77)\r\n15 xxx                     0x1003e9bf4 __30-[FMDatabaseQueue inDatabase:]_block_invoke (FMDatabaseQueue.m:162)\r\n16 libdispatch.dylib              0x18364547c _dispatch_client_callout + 16\r\n17 libdispatch.dylib              0x183650480 _dispatch_barrier_sync_f_slow + 824\r\n18 xxx                     0x1003e9b64 -[FMDatabaseQueue inDatabase:] (FMDatabaseQueue.m:176)\r\n19 xxx                     0x1003e1344 -[FIMUserCRUD insertItem:inDatabase:] (FIMUserCRUD.m:78)\r\n20 xxx                     0x1003cfe44 -[FIMCacheManager insertMessage:] (FIMCacheManager.m:63)\r\n```\r\n\r\nthis is insert code\r\n```objective-c\r\n- (void)insertMessage:(FIMMessageModel *)message {\r\n    FIMCacheMessage *cacheMessage = [[FIMCacheMessage alloc] initWithFIMMessageModel:message];\r\n    [[FIMMessageCRUD shareInstance] insertItem:cacheMessage inDatabase:self.databaseQueue];\r\n    [[FIMUserCRUD shareInstance] insertItem:cacheMessage inDatabase:self.databaseQueue];\r\n}\r\n```\r\n\r\nthis is insert message code\r\n```objective-c\r\n- (void)insertItem:(FIMCacheMessage *)model inDatabase:(FMDatabaseQueue *)databaseQueue {\r\n    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{\r\n        [databaseQueue inDatabase:^(FMDatabase *database) {\r\n            if (![database open]) {\r\n                return;\r\n            }\r\n            NSString *sqlString =\r\n            [NSString stringWithFormat:@\"INSERT INTO %@ (\"\r\n                                       @\"msg_id, msg_create_time, msg_operate_time, \"\r\n                                       @\"msg_is_self, msg_status_code, kdt_id, \"\r\n                                       @\"req_id, user_id, msg_type, \"\r\n                                       @\"msg_content, conversation_id, msg_resource_path, \"\r\n                                       @\"automate, isEvent\"\r\n                                       @\") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)\",\r\n                                       MESSAGE_TABLE_NAME];\r\n\r\n            [database executeUpdate:sqlString, @(model.messageID), @(model.createTime),\r\n                                    @(model.opearteTime), @(model.isSelf), @(model.statusCode),\r\n                                    model.kdtID ?: [NSNull null], model.requestID ?: [NSNull null],\r\n                                    model.userID ?: [NSNull null], model.messageType ?: [NSNull null],\r\n                                    model.content ?: [NSNull null], model.conversationID ?: [NSNull null],\r\n                                    model.resourcePath ?: [NSNull null], @(model.automate), @(model.isEvent)];\r\n\r\n            NSLog(@\"MessageCRUD insertItem %@ errorMsg: %@\", model, [database lastErrorMessage]);\r\n\r\n            [database close];\r\n        }];\r\n    });\r\n}\r\n```\r\n\r\nthis is insert user info code\r\n```objective-c\r\n- (void)insertItem:(FIMCacheMessage *)model inDatabase:(FMDatabaseQueue *)databaseQueue {\r\n    [databaseQueue inDatabase:^(FMDatabase *database) {\r\n        if (![database open]) {\r\n            return;\r\n        }\r\n\r\n        NSString *querySQLString = [NSString\r\n        stringWithFormat:@\"SELECT * FROM %@ WHERE user_id = '%@'\", USER_TABLE_NAME, model.userID];\r\n\r\n        FMResultSet *resultSet = [database executeQuery:querySQLString];\r\n        if ([resultSet next]) {\r\n            NSString *avatar = [resultSet stringForColumn:@\"user_avatar\"];\r\n            NSString *nickname = [resultSet stringForColumn:@\"user_nickname\"];\r\n            if (![avatar isEqualToString:model.userModel.userAvatar] ||\r\n                ![nickname isEqualToString:model.userModel.userNickname]) {\r\n                [self updateItem:model.userModel inDatabase:databaseQueue];\r\n            }\r\n        } else {\r\n            NSString *sqlString = [NSString stringWithFormat:@\"INSERT INTO %@ (\"\r\n                                                             @\"user_id, user_nickname, user_avatar\"\r\n                                                             @\") VALUES (?,?,?)\",\r\n                                                             USER_TABLE_NAME];\r\n\r\n            [database executeUpdate:sqlString, model.userModel.userID ?: [NSNull null],\r\n                                    model.userModel.userNickname ?: [NSNull null],\r\n                                    model.userModel.userAvatar ?: [NSNull null]];\r\n\r\n            NSLog(@\"UserCRUD insertItem %@ errorMsg: %@\", model, [database lastErrorMessage]);\r\n        }\r\n\r\n        [database close];\r\n    }];\r\n}\r\n```\r\n\r\nwhat cause it?"},"comment":{"url":"https://api.github.com/repos/ccgus/fmdb/issues/comments/242401947","html_url":"https://github.com/ccgus/fmdb/issues/521#issuecomment-242401947","issue_url":"https://api.github.com/repos/ccgus/fmdb/issues/521","id":242401947,"user":{"login":"Yuzeyang","id":12770548,"avatar_url":"https://avatars.githubusercontent.com/u/12770548?v=3","gravatar_id":"","url":"https://api.github.com/users/Yuzeyang","html_url":"https://github.com/Yuzeyang","followers_url":"https://api.github.com/users/Yuzeyang/followers","following_url":"https://api.github.com/users/Yuzeyang/following{/other_user}","gists_url":"https://api.github.com/users/Yuzeyang/gists{/gist_id}","starred_url":"https://api.github.com/users/Yuzeyang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Yuzeyang/subscriptions","organizations_url":"https://api.github.com/users/Yuzeyang/orgs","repos_url":"https://api.github.com/users/Yuzeyang/repos","events_url":"https://api.github.com/users/Yuzeyang/events{/privacy}","received_events_url":"https://api.github.com/users/Yuzeyang/received_events","type":"User","site_admin":false},"created_at":"2016-08-25T14:14:54Z","updated_at":"2016-08-25T14:14:54Z","body":"i see the other issues about crash in `[FMDatabase executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:] line:960 sqlite3_prepare_v2 exc_bad_access`,and you suggest them to handle multithread `in database:` or `in inTransaction `,and i do it\r\n\r\nthis is my insert list code\r\n```objective-c\r\n- (void)insertList:(NSArray<FIMCacheMessage *> *)modelList\r\n        inDatabase:(FMDatabaseQueue *)databaseQueue {\r\n    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{\r\n        [databaseQueue inTransaction:^(FMDatabase *database, BOOL *rollback) {\r\n            for (FIMCacheMessage *model in modelList) {\r\n                NSString *sqlString =\r\n                [NSString stringWithFormat:@\"INSERT INTO %@ (\"\r\n                                           @\"msg_id, msg_create_time, msg_operate_time, \"\r\n                                           @\"msg_is_self, msg_status_code, kdt_id, \"\r\n                                           @\"req_id, user_id, msg_type, \"\r\n                                           @\"msg_content, conversation_id, msg_resource_path, \"\r\n                                           @\"automate, isEvent\"\r\n                                           @\") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)\",\r\n                                           MESSAGE_TABLE_NAME];\r\n\r\n                BOOL isSucceed = [database\r\n                executeUpdate:sqlString, @(model.messageID), @(model.createTime),\r\n                              @(model.opearteTime), @(model.isSelf), @(model.statusCode),\r\n                              model.kdtID ?: [NSNull null], model.requestID ?: [NSNull null],\r\n                              model.userID ?: [NSNull null], model.messageType ?: [NSNull null],\r\n                              model.content ?: [NSNull null], model.conversationID ?: [NSNull null],\r\n                              model.resourcePath ?: [NSNull null], @(model.automate), @(model.isEvent)];\r\n\r\n                NSLog(@\"MessageCRUD insertList %@ %@ errorMsg: %@\", modelList,\r\n                      isSucceed ? @\"成功\" : @\"失败\", [database lastErrorMessage]);\r\n\r\n                if (!isSucceed) {\r\n                    *rollback = YES;\r\n                    return;\r\n                }\r\n            }\r\n        }];\r\n    });\r\n}\r\n```\r\n\r\nthe `databaseQueue` is a singleton's databasequeue,and i create it once,\r\nbut according to the fabric's statistics ,there are still some crashes\r\ni cannot understand i put the insert handle in a async queue,and in Transaction still lead to crash Small probability\r\n\r\nthis is the crash log\r\n```\r\n0  libsqlite3.dylib               0x183069fbc (null) + 9096\r\n1  libsqlite3.dylib               0x1830b3678 (null) + 86248\r\n2  libsqlite3.dylib               0x1830b3678 (null) + 86248\r\n3  libsqlite3.dylib               0x183026270 (null) + 16012\r\n4  libsqlite3.dylib               0x18302459c (null) + 8632\r\n5  libsqlite3.dylib               0x18302369c (null) + 4792\r\n6  libsqlite3.dylib               0x183022f0c (null) + 2856\r\n7  libsqlite3.dylib               0x183022bc4 (null) + 2016\r\n8  xxx                     0x100468100 -[FMDatabase executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:] (FMDatabase.m:960)\r\n9  xxx                     0x100468a54 -[FMDatabase executeUpdate:] (FMDatabase.m:1158)\r\n10 xxx                     0x10045ce04 __40-[FIMMessageCRUD insertList:inDatabase:]_block_invoke_2 (FIMMessageCRUD.m:106)\r\n11 xxx                     0x10046bf20 __46-[FMDatabaseQueue beginTransaction:withBlock:]_block_invoke (FMDatabaseQueue.m:192)\r\n12 libdispatch.dylib              0x18259d47c _dispatch_client_callout + 16\r\n13 libdispatch.dylib              0x1825a8728 _dispatch_barrier_sync_f_invoke + 100\r\n14 xxx                     0x10046be60 -[FMDatabaseQueue beginTransaction:withBlock:] (FMDatabaseQueue.m:203)\r\n15 xxx                     0x10045c80c __40-[FIMMessageCRUD insertList:inDatabase:]_block_invoke (FIMMessageCRUD.m:125)\r\n16 libdispatch.dylib              0x18259d4bc _dispatch_call_block_and_release + 24\r\n17 libdispatch.dylib              0x18259d47c _dispatch_client_callout + 16\r\n18 libdispatch.dylib              0x1825ab914 _dispatch_root_queue_drain + 2140\r\n19 libdispatch.dylib              0x1825ab0b0 _dispatch_worker_thread3 + 112\r\n20 libsystem_pthread.dylib        0x1827b5470 _pthread_wqthread + 1092\r\n21 libsystem_pthread.dylib        0x1827b5020 start_wqthread + 4\r\n``` \r\n"}}
{"actor":{"display_login":"Yuzeyang"}}