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