Skip to content

Commit 5fcb545

Browse files
authored
fix: remove javapart ref from cpp nativeproxy (#2934)
PR analogous to software-mansion/react-native-reanimated#7515 regarding the `javaPart_`. Right now when you do a reload in a simple app with screens installed, you will see in AS profiler that instances of all NativeProxys are still in the memory. It is caused by `javaPart_` never being released thus keeping java part after reload. I also removed empty destructor of `NativeProxy`.
1 parent 0564d43 commit 5fcb545

File tree

5 files changed

+13
-5
lines changed

5 files changed

+13
-5
lines changed

android/src/fabric/java/com/swmansion/rnscreens/NativeProxy.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class NativeProxy {
2020

2121
external fun nativeAddMutationsListener(fabricUIManager: FabricUIManager)
2222

23+
external fun invalidateNative()
24+
2325
companion object {
2426
// we use ConcurrentHashMap here since it will be read on the JS thread,
2527
// and written to on the UI thread.

android/src/main/cpp/NativeProxy.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ namespace rnscreens {
1414
NativeProxy::NativeProxy(jni::alias_ref<NativeProxy::javaobject> jThis)
1515
: javaPart_(jni::make_global(jThis)) {}
1616

17-
NativeProxy::~NativeProxy() {}
18-
1917
void NativeProxy::registerNatives() {
2018
registerHybrid(
2119
{makeNativeMethod("initHybrid", NativeProxy::initHybrid),
2220
makeNativeMethod(
2321
"nativeAddMutationsListener",
24-
NativeProxy::nativeAddMutationsListener)});
22+
NativeProxy::nativeAddMutationsListener),
23+
makeNativeMethod("invalidateNative", NativeProxy::invalidateNative)});
2524
}
2625

2726
void NativeProxy::nativeAddMutationsListener(
@@ -48,4 +47,8 @@ jni::local_ref<NativeProxy::jhybriddata> NativeProxy::initHybrid(
4847
return makeCxxInstance(jThis);
4948
}
5049

50+
void NativeProxy::invalidateNative() {
51+
javaPart_ = nullptr;
52+
}
53+
5154
} // namespace rnscreens

android/src/main/cpp/NativeProxy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
1919
jni::alias_ref<jhybridobject> jThis);
2020
static void registerNatives();
2121

22-
~NativeProxy();
23-
2422
private:
2523
friend HybridBase;
2624
jni::global_ref<NativeProxy::javaobject> javaPart_;
@@ -30,6 +28,8 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
3028
void nativeAddMutationsListener(
3129
jni::alias_ref<facebook::react::JFabricUIManager::javaobject>
3230
fabricUIManager);
31+
32+
void invalidateNative();
3333
};
3434

3535
} // namespace rnscreens

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ScreensModule(
3939

4040
override fun invalidate() {
4141
super.invalidate()
42+
proxy?.invalidateNative();
4243
nativeUninstall()
4344
}
4445

android/src/paper/java/com/swmansion/rnscreens/NativeProxy.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class NativeProxy {
66
// do nothing on Paper
77
fun nativeAddMutationsListener(fabricUIManager: FabricUIManager) = Unit
88

9+
fun invalidateNative() = Unit
10+
911
companion object {
1012
fun addScreenToMap(
1113
tag: Int,

0 commit comments

Comments
 (0)