ibireme / YYCache

High performance cache framework for iOS.
MIT License
2.37k stars 505 forks source link

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

Open li6185377 opened 2 months ago

li6185377 commented 2 months ago

修复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 膨胀的治理。

zhenghaonagehao commented 1 week ago

觉着这个问题作者会修复么?

faimin commented 1 week ago

觉着这个问题作者会修复么?

@zhenghaonagehao li6185377老哥把解决办法贴在这里的初衷也许并不是奢望作者及时的进行修复,而只是给遇到同样问题的朋友们提供一种解决方案,反哺开源社区~

kedu commented 5 days ago

能列全一下代码吗?不知道替换哪些代码

miaoruiyuan commented 4 days ago

能列全一下代码吗?不知道替换哪些代码 YYKVStorage.m 104 line

kedu commented 4 days ago

我这104行是这 _dbOpenErrorCount = 0; ,肯定不对

li6185377 commented 4 days ago

我这104行是这 _dbOpenErrorCount = 0; ,肯定不对

YYKVStorage.m

// 原代码
    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);
    }
kedu commented 4 days ago

好的,感谢

LoveYao commented 13 hours ago

感谢大佬

Rogue24 commented 12 hours ago

@li6185377 大佬,替换后会有警告 “Initializing 'sqlite3_stmt ' (aka 'struct sqlite3_stmt ') with an expression of type 'const sqlite3_stmt ' (aka 'const struct sqlite3_stmt ') discards qualifiers”,会有影响吗?