Conversation
…shing `MMKV::removeStorage()` closes and deletes the file's native MMKV instance, but every `HybridMMKV` of that file kept its raw pointer to it. Any later call used freed memory, and `createMMKV()`'s AppState listener makes one (`checkContentChanged()`) the next time the app comes to the foreground: a crash or a hang. `HybridMMKV` now keeps a registry of live instances. `deleteMMKV()` unlinks every instance of the file before deleting it: using one then throws, while `checkContentChanged()` and `trim()` (called by the listeners) do nothing, and `id` stays readable. Fixes margelo#1082
mrousavy
requested changes
Oct 2, 2026
mrousavy
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR but the static instances is a problem imo
Comment on lines
+16
to
+17
| std::mutex HybridMMKV::_liveInstancesMutex; | ||
| std::unordered_set<HybridMMKV*> HybridMMKV::_liveInstances; |
Member
There was a problem hiding this comment.
I want to avoid keeping track of such static maps. That's the whole point of object-oriented programming to not do that.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1082
Problem
deleteMMKV(id)callsMMKV::removeStorage(id), which closes the file's native instance (MMKV::close()ends indelete this). EveryHybridMMKVof that file keeps its rawMMKV*, so any later call on it uses freed memory. The app doesn't even have to touch the object again: the AppState listener added bycreateMMKV()callscheckContentChanged()on it the next time the app becomes active, which crashes (#1082 on iOS; on Android aSIGSEGVinpthread_mutex_lockunderMMKV::checkContentChanged()) or hangs.Fix
No changes needed in MMKV Core:
HybridMMKVkeeps a registry of live instances (a mutex-guarded set, added in the constructor, removed in the destructor).deleteMMKV(id)callsHybridMMKV::invalidateInstances(id)beforeMMKV::removeStorage(id). It finds the native instance of that file (id + root path, resolving an empty path to MMKV's root directory) and unlinks everyHybridMMKVthat points to it. MMKV caches one native instance per file, so this also covers severalcreateMMKV()calls for the same id.The MMKV instance "…" has been deleted with deleteMMKV(...)! Create a new one with createMMKV(...).;checkContentChanged()andtrim()do nothing, since the AppState and memory-warning listeners call them;idstays readable.createMMKV()with the same id afterwards works as before.invalidateInstances()takes an optional root path, so it should fit #1070:deleteMMKV(id, path)would pass its path through.Tests
New harness test, "should make live instances unusable after deleteMMKV()". It deletes a storage while two instances of it are alive, then checks that:
Ran on an Android emulator (API 37, x86_64), the same way as the
harness-androidworkflow:Also:
bun run typecheck,bun run lint-ci,bun run testand the clang-format 18 check pass. The Android build has no new warnings.Not tested on iOS (no Mac here). The change is in shared C++ only.