From b91184885faf26e684c82aeb2ac99c5a75bd9ca2 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Thu, 2 Aug 2018 03:50:16 -0700 Subject: [PATCH] Fix behaviour of `freeNatives()` Summary: @public Prevents repeated deallocation of weak references. Reviewed By: pasqualeanatriello Differential Revision: D9131551 fbshipit-source-id: bc79596e056ae0657a55146ad786422fd0f5badc --- .../facebook/yoga/YogaNodePropertiesByteBuffer.java | 12 ++++++++---- .../com/facebook/yoga/YogaNodePropertiesJNI.java | 5 +++-- .../src/main/jni/first-party/yogajni/jni/YGJNI.cpp | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesByteBuffer.java b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesByteBuffer.java index b792062bfe4b99..daca67dba1dd8c 100644 --- a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesByteBuffer.java +++ b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesByteBuffer.java @@ -25,6 +25,7 @@ public class YogaNodePropertiesByteBuffer implements YogaNodeProperties, Cloneab private final long mNativePointer; private boolean mHasBorderSet = false; private boolean mHasNewLayout = true; + private boolean isFreed = false; private static native ByteBuffer jni_getStyleBuffer(long nativePointer); @@ -48,12 +49,10 @@ public YogaNodePropertiesByteBuffer(long nativePointer) { mLayoutBuffer = jni_getLayoutBuffer(nativePointer).order(ByteOrder.LITTLE_ENDIAN); } - private static native void jni_YGNodeFree(long nativePointer); - @Override protected void finalize() throws Throwable { try { - jni_YGNodeFree(getNativePointer()); + freeNatives(); } finally { super.finalize(); } @@ -482,9 +481,14 @@ public YogaDirection getLayoutDirection() { return YogaDirection.fromInt(getLayoutDirectionInt()); } + private static native void jni_YGNodeFree(long nativePointer); + @Override public void freeNatives() { - jni_YGNodeFree(mNativePointer); + if (!isFreed) { + isFreed = true; + jni_YGNodeFree(mNativePointer); + } } private int getLayoutDirectionInt() { diff --git a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesJNI.java b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesJNI.java index d7644ba5991187..2322193c0b3a35 100644 --- a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesJNI.java +++ b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodePropertiesJNI.java @@ -66,12 +66,13 @@ public YogaNodePropertiesJNI(YogaNode node, YogaConfig config) { } } - private native void jni_YGNodeFree(long nativePointer); + private static native void jni_YGNodeFree(long nativePointer); @Override public void freeNatives() { - jni_YGNodeFree(mNativePointer); + long nativePointer = mNativePointer; mNativePointer = 0; + jni_YGNodeFree(nativePointer); } @Override diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp index ecf13a26f87ba4..7f4dc04122d741 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp @@ -384,7 +384,7 @@ jlong jni_YGNodeCloneNoProps( return jni_YGNodeClone(cls, nativePointer, clonedJavaObject, nullptr); } -void jni_YGNodeFree(alias_ref thiz, jlong nativePointer) { +void jni_YGNodeFree(alias_ref thiz, jlong nativePointer) { if (nativePointer == 0) { return; }