Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Break circular reference in NativeProxy and AndroidUIScheduler #6697

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lukmccall
Copy link
Member

Summary

Breaks circular reference in NativeProxy and AndroidUIScheduler.
Needs #6671 to work.

NativeProxy and AndroidUIScheduler 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 invalidation of the NativeProxy and AndroidUIScheduler.

Test plan

I tested it with a new project using Expo (SDK 52) and with Fabric enabled. I reloaded the app several times and performed a heap dump to verify that everything was removed correctly.

Comment on lines +278 to +224
// Module might be already destroyed.
if (!javaPart_) {
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of checking this every time, but it sure is the best way if we don't want any refactors here. However, I'd much prefer something among the lines for transparency:

Suggested change
// Module might be already destroyed.
if (!javaPart_) {
return;
}
if (invalidated) {
return;
}

What do you think?

Copy link
Member

@piaskowyk piaskowyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, the approach is okay 👍

@lukmccall lukmccall force-pushed the @lukmccall/memory/break-cycle-ref-in-native-proxy branch from b450de3 to db174c8 Compare January 10, 2025 12:52
@lukmccall lukmccall force-pushed the @lukmccall/memory/break-cycle-ref-in-native-proxy branch from db174c8 to a733aa2 Compare January 10, 2025 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants