From f61fbd269ea8f1210f22fcbb832dd66b598505ec Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Tue, 11 Oct 2016 04:48:47 -0700 Subject: [PATCH] Add finalizer to release any unreleased native memory Summary: Ensure all native memory is released once gc deallocates the java object reference. Reviewed By: lucasr Differential Revision: D3992759 fbshipit-source-id: f648b72ead5bdb7257a5197496b19795f71f3788 --- java/com/facebook/csslayout/CSSNodeJNI.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/java/com/facebook/csslayout/CSSNodeJNI.java b/java/com/facebook/csslayout/CSSNodeJNI.java index 3f770464..e4c30c95 100644 --- a/java/com/facebook/csslayout/CSSNodeJNI.java +++ b/java/com/facebook/csslayout/CSSNodeJNI.java @@ -60,6 +60,10 @@ public class CSSNodeJNI implements CSSNodeAPI { throw new IllegalStateException("You should not reset an attached CSSNode"); } + free(); + } + + private void free() { jni_CSSNodeFree(mNativePointer); mNativePointer = 0; mChildren = null; @@ -67,6 +71,17 @@ public class CSSNodeJNI implements CSSNodeAPI { mMeasureFunction = null; } + @Override + protected void finalize() throws Throwable { + try { + if (mNativePointer != 0) { + free(); + } + } finally { + super.finalize(); + } + } + @Override public int getChildCount() { return mChildren.size();