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 boolean mHasBorderSet = false;
private boolean mHasNewLayout = true;
private boolean isFreed = false;
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);
}
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 class YogaNodePropertiesByteBuffer implements YogaNodeProperties, Cloneab
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() {

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
public void freeNatives() {
jni_YGNodeFree(mNativePointer);
long nativePointer = mNativePointer;
mNativePointer = 0;
jni_YGNodeFree(nativePointer);
}
@Override

View File

@ -384,7 +384,7 @@ jlong jni_YGNodeCloneNoProps(
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) {
return;
}