问题原因
升级Xcode 16 之后,真机运行APP,发现会有Crash,崩溃堆栈线上Crash 在 YYCache
之中。如下图所示
崩溃堆栈如下:
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRTframe #0: 0x00000001d93911d4 libsystem_kernel.dylib`__pthread_kill + 8frame #1: 0x00000002114d6ef8 libsystem_pthread.dylib`pthread_kill + 268frame #2: 0x0000000190d83ad8 libsystem_c.dylib`abort + 128frame #3: 0x0000000190d82e08 libsystem_c.dylib`__assert_rtn + 284frame #4: 0x000000010500f7d8 libRPAC.dylib`isBulkReadStatement.cold.3 + 100frame #5: 0x0000000104f80c88 libRPAC.dylib`isBulkReadStatement + 360frame #6: 0x0000000105005254 libRPAC.dylib`interposed_sqlite3_finalize + 152* frame #7: 0x0000000111d7adc0 CsdnPlus.debug.dylib`-[YYKVStorage _dbClose](self=0x000000013448ccc0, _cmd="_dbClose") at YYKVStorage.m:138:21frame #8: 0x0000000111d7ee84 CsdnPlus.debug.dylib`-[YYKVStorage dealloc](self=0x000000013448ccc0, _cmd="dealloc") at YYKVStorage.m:731:5frame #9: 0x0000000111d77d8c CsdnPlus.debug.dylib`-[YYDiskCache _appWillBeTerminated](self=0x0000000134f91200, _cmd="_appWillBeTerminated") at YYDiskCache.m:155:9frame #10: 0x0000000188fc32f4 CoreFoundation`__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 148frame #11: 0x0000000188fc3210 CoreFoundation`___CFXRegistrationPost_block_invoke + 88frame #12: 0x0000000188fc3158 CoreFoundation`_CFXRegistrationPost + 436frame #13: 0x0000000188fc170c CoreFoundation`_CFXNotificationPost + 728frame #14: 0x0000000187be4ea4 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 92frame #15: 0x000000018c897df0 UIKitCore`-[UIApplication _terminateWithStatus:] + 232frame #16: 0x000000018b8d6f88 UIKitCore`-[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 112frame #17: 0x000000018c0b78f4 UIKitCore`-[_UISceneLifecycleMultiplexer forceExitWithTransitionContext:scene:] + 164frame #18: 0x000000018c89354c UIKitCore`-[UIApplication workspaceShouldExit:withTransitionContext:] + 164frame #19: 0x00000001a25d1f68 FrontBoardServices`__63-[FBSWorkspaceScenesClient willTerminateWithTransitionContext:]_block_invoke_2 + 72frame #20: 0x00000001a2591c44 FrontBoardServices`-[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 168frame #21: 0x00000001a25d1f08 FrontBoardServices`__63-[FBSWorkspaceScenesClient willTerminateWithTransitionContext:]_block_invoke + 128frame #22: 0x000000010563a71c libdispatch.dylib`_dispatch_client_callout + 20frame #23: 0x000000010563e13c libdispatch.dylib`_dispatch_block_invoke_direct + 296frame #24: 0x00000001a2593300 FrontBoardServices`__FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 52frame #25: 0x00000001a2593280 FrontBoardServices`-[FBSMainRunLoopSerialQueue _targetQueue_performNextIfPossible] + 240frame #26: 0x00000001a2593158 FrontBoardServices`-[FBSMainRunLoopSerialQueue _performNextFromRunLoopSource] + 28frame #27: 0x0000000188fc8328 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28frame #28: 0x0000000188fc82bc CoreFoundation`__CFRunLoopDoSource0 + 176frame #29: 0x0000000188fc5dc0 CoreFoundation`__CFRunLoopDoSources0 + 244frame #30: 0x0000000188fc4fbc CoreFoundation`__CFRunLoopRun + 840frame #31: 0x0000000188fc4830 CoreFoundation`CFRunLoopRunSpecific + 588frame #32: 0x00000001d4fa41c4 GraphicsServices`GSEventRunModal + 164frame #33: 0x000000018bb2aeb0 UIKitCore`-[UIApplication _run] + 816frame #34: 0x000000018bbd95b4 UIKitCore`UIApplicationMain + 340frame #35: 0x000000010fae959c CsdnPlus.debug.dylib`main(argc=3, argv=0x000000016b2e7438) at main.m:14:16frame #36: 0x00000001ae9b2ec8 dyld`start + 2724
解决方案一
@implementation YYKVStorage- (BOOL)_dbClose {...// 原代码if (_dbStmtCache) CFRelease(_dbStmtCache);// 替换为if (_dbStmtCache) { CFIndex size = CFDictionaryGetCount(_dbStmtCache);CFTypeRef *valuesRef = (CFTypeRef *)malloc(size * sizeof(CFTypeRef));CFDictionaryGetKeysAndValues(_dbStmtCache, NULL, (const void **)valuesRef);const sqlite3_stmt **stmts = (const sqlite3_stmt **)valuesRef;for (CFIndex i = 0; i < size; i ++) {sqlite3_stmt *stmt = stmts[i];sqlite3_finalize(stmt);}free(valuesRef);CFRelease(_dbStmtCache);}...
}
解决方案二
static void _finalizeStatement(const void *key, const void *value, void *context) {sqlite3_finalize((sqlite3_stmt *)value);
}- (BOOL)_dbClose {···// 原代码if (_dbStmtCache) CFRelease(_dbStmtCache);// 替换为if (_dbStmtCache) { CFDictionaryApplyFunction(_dbStmtCache, _finalizeStatement, NULL);CFRelease(_dbStmtCache);}···
}