fmdb icon indicating copy to clipboard operation
fmdb copied to clipboard

Avoiding crash in FMResultSet

Open JasF opened this issue 6 years ago • 2 comments

Hi Guys, I found the crash and solution. Just add the @autorelease { } to method FMDatabaseQueue.m:

  • (void)beginTransaction:(FMDBTransaction)transaction withBlock:(void (^)(FMDatabase *db, BOOL *rollback))block { inside in dispatch_sync(_queue, ^() { block.

In my project I have 25 tables, each in different .db files, and 25 FMDatabaseQueue. Application can intensively read from databases (25 parallel requests from many threads). If @autoreleasepool missing in beginTransaction, the ARC can manage deallocation of FMResultSet after main method will finished (after exiting from RunLoop). In dealloc, FMResultSet has access to database, but already other FMDatabaseQueue can have exclusive access to sql and it produce crash.

If you add @autoreleasepool inside beginTransaction, all FMResultSet objects will be deallocated exactly after exiting from block() or after exiting from beginTransaction method and not be crashed app, when other FMDatabaseQueue has access. =)

JasF avatar May 26 '18 12:05 JasF

Why wouldn't you add the autorelease inside the block you're using?

ccgus avatar Jun 11 '18 19:06 ccgus

I also met same issue,and it only happened on iPhone X(iOS 12 or iOS 11.4),is it the same to you?

thread stacktrace attached belowed: 1upchat 0x100521ea0 pcache1FetchStage2 (sqlite3.c:46418) 2upchat 0x10052184c pcache1Fetch (sqlite3.c:46558) 3upchat 0x10052184c pcache1Fetch (sqlite3.c:46558) 4upchat 0x1004adeac sqlite3PagerGet (sqlite3.c:45121) 5upchat 0x1004df6cc getAndInitPage (sqlite3.c:60604) 6upchat 0x1004dd790 sqlite3BtreeMovetoUnpacked (sqlite3.c:63879) 7upchat 0x1004def44 handleDeferredMoveto (sqlite3.c:73803) 8upchat 0x1004d9c8c sqlite3VdbeExec (sqlite3.c:73867) 9upchat 0x1004b6d3c sqlite3_step (sqlite3.c:75900) 10upchat 0x1005a008c -[FMResultSet nextWithError:] (FMResultSet.m:161) 11upchat 0x1002c095c __44+[UPCSQLPreferencesHelper preferenceForKey:]_block_invoke (UPCSQLPreferencesHelper.m:107) 12upchat 0x10059d4f4 __30-[FMDatabaseQueue inDatabase:]_block_invoke (FMDatabaseQueue.m:162) 13libdispatch.dylib0x180839474 14libdispatch.dylib0x18081906c 15upchat 0x10059d464 -[FMDatabaseQueue inDatabase:] (FMDatabaseQueue.m:176) 16upchat 0x1002c0880 +[UPCSQLPreferencesHelper preferenceForKey:] (UPCSQLPreferencesHelper.m:111) 17upchat 0x1000d7358 -[UPCWelcomeViewController shouldShowGuide] (UPCWelcomeViewController.m:0) 18upchat 0x1000d511c -[UPCWelcomeViewController viewDidLoad] (UPCWelcomeViewController.m:64)

zhaocaimaocnm avatar Jul 09 '18 06:07 zhaocaimaocnm