Fix behaviour of `freeNatives()`

Summary:
@public

Prevents repeated deallocation of weak references.

Reviewed By: pasqualeanatriello

Differential Revision: D9131551

fbshipit-source-id: bc79596e056ae0657a55146ad786422fd0f5badc
This commit is contained in:
David Aurelio 2018-08-02 03:50:16 -07:00 committed by Facebook Github Bot
parent cdf9b84dd0
commit b91184885f
3 changed files with 12 additions and 7 deletions

View File

@ -25,6 +25,7 @@ public class YogaNodePropertiesByteBuffer implements YogaNodeProperties, Cloneab
private final long mNativePointer; private final long mNativePointer;
private boolean mHasBorderSet = false; private boolean mHasBorderSet = false;
private boolean mHasNewLayout = true; private boolean mHasNewLayout = true;
private boolean isFreed = false;
private static native ByteBuffer jni_getStyleBuffer(long nativePointer); private static native ByteBuffer jni_getStyleBuffer(long nativePointer);
@ -48,12 +49,10 @@ public class YogaNodePropertiesByteBuffer implements YogaNodeProperties, Cloneab
mLayoutBuffer = jni_getLayoutBuffer(nativePointer).order(ByteOrder.LITTLE_ENDIAN); mLayoutBuffer = jni_getLayoutBuffer(nativePointer).order(ByteOrder.LITTLE_ENDIAN);
} }
private static native void jni_YGNodeFree(long nativePointer);
@Override @Override
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
try { try {
jni_YGNodeFree(getNativePointer()); freeNatives();
} finally { } finally {
super.finalize(); super.finalize();
} }
@ -482,10 +481,15 @@ public class YogaNodePropertiesByteBuffer implements YogaNodeProperties, Cloneab
return YogaDirection.fromInt(getLayoutDirectionInt()); return YogaDirection.fromInt(getLayoutDirectionInt());
} }
private static native void jni_YGNodeFree(long nativePointer);
@Override @Override
public void freeNatives() { public void freeNatives() {
if (!isFreed) {
isFreed = true;
jni_YGNodeFree(mNativePointer); jni_YGNodeFree(mNativePointer);
} }
}
private int getLayoutDirectionInt() { private int getLayoutDirectionInt() {
return mLayoutBuffer.getInt(YogaNodeMemoryLayout.layoutDirection); return mLayoutBuffer.getInt(YogaNodeMemoryLayout.layoutDirection);

View File

@ -66,12 +66,13 @@ public class YogaNodePropertiesJNI implements Cloneable, YogaNodeProperties {
} }
} }
private native void jni_YGNodeFree(long nativePointer); private static native void jni_YGNodeFree(long nativePointer);
@Override @Override
public void freeNatives() { public void freeNatives() {
jni_YGNodeFree(mNativePointer); long nativePointer = mNativePointer;
mNativePointer = 0; mNativePointer = 0;
jni_YGNodeFree(nativePointer);
} }
@Override @Override

View File

@ -384,7 +384,7 @@ jlong jni_YGNodeCloneNoProps(
return jni_YGNodeClone(cls, nativePointer, clonedJavaObject, nullptr); return jni_YGNodeClone(cls, nativePointer, clonedJavaObject, nullptr);
} }
void jni_YGNodeFree(alias_ref<jobject> thiz, jlong nativePointer) { void jni_YGNodeFree(alias_ref<jclass> thiz, jlong nativePointer) {
if (nativePointer == 0) { if (nativePointer == 0) {
return; return;
} }