YYCache icon indicating copy to clipboard operation
YYCache copied to clipboard

修复 Xcode16 编译运行 iOS18 sqlite3_finalize 闪退问题

Open li6185377 opened this issue 7 months ago • 10 comments

修复Xcode16编译引起的闪退,在 iOS18 中,需要提前对 sqlite3_stmt 执行 sqlite3_finalize

@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);
    }
    ...
}

@end

建议改用 CFDictionaryApplyFunction 更加优雅: https://github.com/ibireme/YYCache/pull/163

该 pull request 还有对 WAL 膨胀的治理。

li6185377 avatar Jul 16 '24 09:07 li6185377