react-native-mmkv icon indicating copy to clipboard operation
react-native-mmkv copied to clipboard

MainApp(73486,0x16e067000) malloc: *** error for object 0x107300012: pointer being freed was not allocated

Open dule-vikki opened this issue 3 weeks ago • 8 comments

Hi @mrousavy, currently I have to migrate the unencrypted data to new encrypt MMKV and I am facing this crash as title in the first time when I open an app using MMKV v4 from an app version using MMKV v2 Please take a look and help me investigate this. Thanks!

My Device:

Model:iPhone 13 Pro
Orientation:Unknown
RAM free: 145.45 MB
Operating System
Version:16.5.0
Orientation:Portrait
Jailbroken: No

Env:

"react-native": "0.78.3",
newArch: false,

// old mmkv
"react-native-mmkv": "2.12.2",

// new mmkv
"react-native-mmkv": "4.1.0",
"react-native-nitro-modules": "0.31.10",

This is my code:

import Keys from 'react-native-keys';
import { createMMKV, deleteMMKV, MMKV } from 'react-native-mmkv';

class Storage {
  localInstance: MMKV = createMMKV({ id: STORAGE_NAME });
  newLocalInstance: MMKV = createMMKV({
    id: NEW_STORAGE_NAME,
    encryptionKey: Keys.secureFor('STORAGE_ENCRYPTION_KEY'),
  });
  memory: Record<string, any> = {};

  migrateData = () => {
    try {
      // all data were be migrated
      if (this.localInstance.getAllKeys().length === 0) {
        return;
      }

      // all data are migrating...
      this.newLocalInstance.importAllFrom(this.localInstance);

      // migration is finish
      this.localInstance.clearAll();
      this.localInstance.trim();
      deleteMMKV(this.localInstance.id);
    } catch (error) {
      if (__DEV__) {
        console.log('STORAGE: migration failed: ', error);
      }
    }
  };

  constructor() {
    this.migrateData();
  }
}

const instance = new Storage();
export { instance as Storage };

This is stacktrace file: crash_session_6e951140de724b0fbb2d522d424d0734_DNE_0_v2_stacktrace.txt

dule-vikki avatar Dec 06 '25 13:12 dule-vikki

Guten Tag, Hans here.

[!NOTE] New features, bugfixes, updates and other improvements are all handled mostly by @mrousavy in his free time. To support @mrousavy, please consider 💖 sponsoring him on GitHub 💖. Sponsored issues will be prioritized.

maintenance-hans[bot] avatar Dec 06 '25 13:12 maintenance-hans[bot]

Where is the crash? In deleteMMKV?

mrousavy avatar Dec 06 '25 14:12 mrousavy

Hi @mrousavy, I tried to debug, the importAllFrom() caused this crash. If I use only deleteMMKV() its work without any crash.

dule-vikki avatar Dec 06 '25 15:12 dule-vikki

Hm probably has something to do with encryption... Idk maybe this is an actual bug in the MMKV repo. It works in my tests without encryption (see the Harness tests - those go green) https://github.com/mrousavy/react-native-mmkv/blob/4803e06d72e9e5723f6f0ae2393e1a22000df0bf/example/tests/MMKV.harness.ts#L418

mrousavy avatar Dec 07 '25 11:12 mrousavy

Can you add more tests like my case?

dule-vikki avatar Dec 07 '25 11:12 dule-vikki

Can you? This is an open-source repo

mrousavy avatar Dec 07 '25 11:12 mrousavy

Can you add more tests like my case?

Can you just comment deleteMMKV(this.localInstance.id); from your code and see if it actually fixes the issue

riteshshukla04 avatar Dec 07 '25 17:12 riteshshukla04

Can you add more tests like my case?

Can you just comment deleteMMKV(this.localInstance.id); from your code and see if it actually fixes the issue

I tried but crash still happen

dule-vikki avatar Dec 08 '25 00:12 dule-vikki