From 97fef59f96b84b94280df9283f2183b23f0cccf3 Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Mon, 24 Oct 2016 12:26:56 -0700 Subject: [PATCH] Dont hold strong reference to java objects creating a cycle Summary: Use weak reference to avoid cycle Reviewed By: splhack Differential Revision: D4064773 fbshipit-source-id: 4088fef5e088a8415747898ef17851e21ada5180 --- java/jni/CSSJNI.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/java/jni/CSSJNI.cpp b/java/jni/CSSJNI.cpp index b35f19e6..1e3e2026 100644 --- a/java/jni/CSSJNI.cpp +++ b/java/jni/CSSJNI.cpp @@ -15,7 +15,7 @@ using namespace facebook::jni; using namespace std; static void _jniPrint(void *context) { - const auto obj = wrap_alias(reinterpret_cast(context)); + auto obj = adopt_local(Environment::current()->NewLocalRef(reinterpret_cast(context))); cout << obj->toString() << endl; } @@ -24,7 +24,7 @@ static CSSSize _jniMeasureFunc(void *context, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode) { - const auto obj = wrap_alias(reinterpret_cast(context)); + auto obj = adopt_local(Environment::current()->NewLocalRef(reinterpret_cast(context))); static auto measureFunc = obj->getClass()->getMethod("measure"); const auto measureResult = measureFunc(obj, width, widthMode, height, heightMode); @@ -34,6 +34,7 @@ static CSSSize _jniMeasureFunc(void *context, const float measuredWidth = static_cast(0xFFFFFFFF & (measureResult >> 32)); const float measuredHeight = static_cast(0xFFFFFFFF & measureResult); + return CSSSize{measuredWidth, measuredHeight}; } @@ -47,15 +48,14 @@ jint jni_CSSNodeGetInstanceCount(alias_ref clazz) { jlong jni_CSSNodeNew(alias_ref thiz) { const CSSNodeRef node = CSSNodeNew(); - auto globalThiz = make_global(thiz); - CSSNodeSetContext(node, globalThiz.release()); + CSSNodeSetContext(node, Environment::current()->NewWeakGlobalRef(thiz.get())); CSSNodeSetPrintFunc(node, _jniPrint); return reinterpret_cast(node); } void jni_CSSNodeFree(alias_ref thiz, jlong nativePointer) { - const auto globalContext = - adopt_global(reinterpret_cast(CSSNodeGetContext(_jlong2CSSNodeRef(nativePointer)))); + Environment::current()->DeleteWeakGlobalRef( + reinterpret_cast(CSSNodeGetContext(_jlong2CSSNodeRef(nativePointer)))); CSSNodeFree(_jlong2CSSNodeRef(nativePointer)); }