Skip to content

Commit 69e2fc4

Browse files
committed
Release javaPart on module invalidate
1 parent 6f3054e commit 69e2fc4

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?) :
149149
private external fun initHybrid(): HybridData
150150
private external fun getBindingsInstallerCxx(): BindingsInstallerHolder
151151
private external fun decorateUIRuntime(): Boolean
152+
private external fun invalidateNative(): Unit
152153

153154
override fun getBindingsInstaller() = getBindingsInstallerCxx()
154155

@@ -166,6 +167,7 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?) :
166167
}
167168
}
168169
}
170+
invalidateNative()
169171
super.invalidate()
170172
}
171173

packages/react-native-gesture-handler/android/src/main/jni/RNGestureHandlerModule.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ void RNGestureHandlerModule::registerNatives() {
2020
"getBindingsInstallerCxx",
2121
RNGestureHandlerModule::getBindingsInstallerCxx),
2222
makeNativeMethod(
23-
"decorateUIRuntime", RNGestureHandlerModule::decorateUIRuntime)});
23+
"decorateUIRuntime", RNGestureHandlerModule::decorateUIRuntime),
24+
makeNativeMethod(
25+
"invalidateNative", RNGestureHandlerModule::invalidateNative)});
2426
}
2527

2628
jni::local_ref<RNGestureHandlerModule::jhybriddata>
@@ -56,4 +58,11 @@ bool RNGestureHandlerModule::decorateUIRuntime() {
5658
this->setGestureState(handlerTag, state);
5759
});
5860
}
61+
62+
void RNGestureHandlerModule::invalidateNative() {
63+
// This is called when the module is being destroyed, so we need to clear
64+
// the reference to the java part to avoid memory leaks.
65+
javaPart_ = nullptr;
66+
}
67+
5968
} // namespace gesturehandler

packages/react-native-gesture-handler/android/src/main/jni/RNGestureHandlerModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ class RNGestureHandlerModule : public jni::HybridClass<RNGestureHandlerModule> {
2929
void setGestureState(const int handlerTag, const int state);
3030
void decorateRuntime(jsi::Runtime &runtime);
3131
bool decorateUIRuntime();
32+
void invalidateNative();
3233
};
3334
} // namespace gesturehandler

0 commit comments

Comments
 (0)