Skip to content

Commit 5b482bc

Browse files
authored
fix: remove global ref when invalidating module (#2914)
## Description <!-- Description and motivation for this PR. Include Fixes #<number> if this is fixing some issue. Fixes # . --> Right now, when removing React Native instance in brownfield apps, the `ScreensModule` cpp side is kept in memory until the whole app is killed. This code removes global ref when invalidating the module too. It fixes memleaks e.g. in Expensify App. ## Changes <!-- Please describe things you've changed here, make a **high level** overview, if change is simple you can omit this section. For example: - Updated `about.md` docs --> <!-- ## Screenshots / GIFs Here you can add screenshots / GIFs documenting your change. You can add before / after section if you're changing some behavior. ### Before ### After --> ## Test code and steps to reproduce <!-- Please include code that can be used to test this change and short description how this example should work. This snippet should be as minimal as possible and ready to be pasted into editor (don't exclude exports or remove "not important" parts of reproduction example) --> You'd need brownfield app so no repro attached. Try remove and add RN instance to native android app a couple of times, then do a heap dump in AS and check for `ScreensModule` instances with 0 depth in its references. ## Checklist - [ ] Included code example that can be used to test this change - [ ] Updated TS types - [ ] Updated documentation: <!-- For adding new props to native-stack --> - [ ] https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md - [ ] https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md - [ ] https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx - [ ] https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx - [ ] Ensured that CI passes
1 parent 53e45a3 commit 5b482bc

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

android/src/main/cpp/jni-adapter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ Java_com_swmansion_rnscreens_ScreensModule_nativeInstall(
9494
std::move(rnScreensModuleHostObject));
9595
}
9696

97+
extern "C" JNIEXPORT void JNICALL
98+
Java_com_swmansion_rnscreens_ScreensModule_nativeUninstall(
99+
JNIEnv *env,
100+
jobject thiz) {
101+
if (globalThis != nullptr) {
102+
env->DeleteGlobalRef(globalThis);
103+
globalThis = nullptr;
104+
}
105+
}
106+
97107
void JNICALL JNI_OnUnload(JavaVM *jvm, void *) {
98108
JNIEnv *env;
99109
if (jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {

android/src/main/java/com/swmansion/rnscreens/ScreensModule.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class ScreensModule(
3535

3636
private external fun nativeInstall(jsiPtr: Long)
3737

38+
private external fun nativeUninstall()
39+
40+
override fun invalidate() {
41+
super.invalidate()
42+
nativeUninstall()
43+
}
44+
3845
override fun initialize() {
3946
super.initialize()
4047
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {

0 commit comments

Comments
 (0)