Skip to content

Commit

Permalink
fix: Break circular reference in LayoutAnimation (#6671)
Browse files Browse the repository at this point in the history
## Summary

Breaks circular reference in the `LayoutAnimations`.

`LayoutAnimations ` is a `HybridClass` with a C++ counterpart holding a
global reference to the Java object. This structure creates a circular
reference between C++ and Java, which the garbage collector cannot clean
up. To resolve this issue, I manually removed the reference between C++
and Java by resetting the global ref during the destructor of the
`NativeProxy`.

## Test plan

- tested in Expo Go ✅

---------

Co-authored-by: Tomasz Żelawski <[email protected]>
  • Loading branch information
lukmccall and tjzel authored Jan 10, 2025
1 parent 333934b commit 3667594
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ jni::local_ref<JArrayInt> LayoutAnimations::getSharedGroup(const int tag) {
return jGroup;
}

void LayoutAnimations::invalidate() {
javaPart_ = nullptr;
}

void LayoutAnimations::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", LayoutAnimations::initHybrid),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class LayoutAnimations : public jni::HybridClass<LayoutAnimations> {
int findPrecedingViewTagForTransition(int tag);
jni::local_ref<JArrayInt> getSharedGroup(const int tag);

void invalidate();

private:
friend HybridBase;
jni::global_ref<LayoutAnimations::javaobject> javaPart_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,10 @@ void NativeProxy::setupLayoutAnimations() {
}

void NativeProxy::invalidateCpp() {
layoutAnimations_->cthis()->invalidate();

workletsModuleProxy_.reset();

if (reanimatedModuleProxy_ != nullptr) {
reanimatedModuleProxy_->invalidate();
}
Expand Down

0 comments on commit 3667594

Please sign in to comment.