Skip to content

Commit

Permalink
Fix YogaConfig getting garbage collected facebook#1678 (facebook#46651)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#46651

Fixes [https://github.com/facebook/yoga/issues/1678](https://github.com/facebook/yoga/issues/1678)

As described in the linked Issue, the problem is that the `YogaConfig` can get garbage collected by the JVM, while a `YogaNode` is still referring to it. This at some point leads to unexpected behaviour (0 values for `layoutWidth`/`layoutHeight`). The change coming with this PR makes sure the `YogaConfig` can not get garbage collected while it's used by a `YogaNode`.

Demo project to confirm the fix https://github.com/michaeltroger/yogabug

Kudos to rtPag, who helped identifying the issue.

X-link: facebook/yoga#1703

Reviewed By: mdvacca

Differential Revision: D63416127

Pulled By: NickGerleman

fbshipit-source-id: efd87dac897e44d3664c228c40cda90f1e11c4f6
  • Loading branch information
michaeltroger authored and facebook-github-bot committed Sep 26, 2024
1 parent 256adca commit 7dcb10b
Showing 1 changed file with 2 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable {
private static final byte LAYOUT_BORDER_START_INDEX = 14;

@Nullable private YogaNodeJNIBase mOwner;
@Nullable private YogaConfig mConfig;
@Nullable private List<YogaNodeJNIBase> mChildren;
@Nullable private YogaMeasureFunction mMeasureFunction;
@Nullable private YogaBaselineFunction mBaselineFunction;
Expand All @@ -57,6 +58,7 @@ private YogaNodeJNIBase(long nativePointer) {

YogaNodeJNIBase(YogaConfig config) {
this(YogaNative.jni_YGNodeNewWithConfigJNI(((YogaConfigJNIBase) config).mNativePointer));
mConfig = config; // makes sure the YogaConfig is not garbage collected
}

public void reset() {
Expand Down

0 comments on commit 7dcb10b

Please sign in to comment.