From 22b018c957e930de950338ad87f4ef8d59e8a169 Mon Sep 17 00:00:00 2001 From: Michael Troger <11340859+michaeltroger@users.noreply.github.com> Date: Wed, 25 Sep 2024 23:53:17 -0700 Subject: [PATCH] Fix YogaConfig getting garbage collected #1678 (#1703) Summary: X-link: https://github.com/facebook/react-native/pull/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. Pull Request resolved: https://github.com/facebook/yoga/pull/1703 Reviewed By: mdvacca Differential Revision: D63416127 Pulled By: NickGerleman fbshipit-source-id: efd87dac897e44d3664c228c40cda90f1e11c4f6 --- java/com/facebook/yoga/YogaNodeJNIBase.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/java/com/facebook/yoga/YogaNodeJNIBase.java b/java/com/facebook/yoga/YogaNodeJNIBase.java index 6e4f9698..a53fe74a 100644 --- a/java/com/facebook/yoga/YogaNodeJNIBase.java +++ b/java/com/facebook/yoga/YogaNodeJNIBase.java @@ -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 mChildren; @Nullable private YogaMeasureFunction mMeasureFunction; @Nullable private YogaBaselineFunction mBaselineFunction; @@ -57,6 +58,7 @@ public abstract class YogaNodeJNIBase extends YogaNode implements Cloneable { YogaNodeJNIBase(YogaConfig config) { this(YogaNative.jni_YGNodeNewWithConfigJNI(((YogaConfigJNIBase) config).mNativePointer)); + mConfig = config; // makes sure the YogaConfig is not garbage collected } public void reset() {