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
This commit is contained in:
Emil Sjolander
2016-10-24 12:26:56 -07:00
committed by Facebook Github Bot
parent e9b9973cae
commit 97fef59f96

View File

@@ -15,7 +15,7 @@ using namespace facebook::jni;
using namespace std; using namespace std;
static void _jniPrint(void *context) { static void _jniPrint(void *context) {
const auto obj = wrap_alias(reinterpret_cast<jobject>(context)); auto obj = adopt_local(Environment::current()->NewLocalRef(reinterpret_cast<jweak>(context)));
cout << obj->toString() << endl; cout << obj->toString() << endl;
} }
@@ -24,7 +24,7 @@ static CSSSize _jniMeasureFunc(void *context,
CSSMeasureMode widthMode, CSSMeasureMode widthMode,
float height, float height,
CSSMeasureMode heightMode) { CSSMeasureMode heightMode) {
const auto obj = wrap_alias(reinterpret_cast<jobject>(context)); auto obj = adopt_local(Environment::current()->NewLocalRef(reinterpret_cast<jweak>(context)));
static auto measureFunc = static auto measureFunc =
obj->getClass()->getMethod<jlong(jfloat, jint, jfloat, jint)>("measure"); obj->getClass()->getMethod<jlong(jfloat, jint, jfloat, jint)>("measure");
const auto measureResult = measureFunc(obj, width, widthMode, height, heightMode); const auto measureResult = measureFunc(obj, width, widthMode, height, heightMode);
@@ -34,6 +34,7 @@ static CSSSize _jniMeasureFunc(void *context,
const float measuredWidth = static_cast<float>(0xFFFFFFFF & (measureResult >> 32)); const float measuredWidth = static_cast<float>(0xFFFFFFFF & (measureResult >> 32));
const float measuredHeight = static_cast<float>(0xFFFFFFFF & measureResult); const float measuredHeight = static_cast<float>(0xFFFFFFFF & measureResult);
return CSSSize{measuredWidth, measuredHeight}; return CSSSize{measuredWidth, measuredHeight};
} }
@@ -47,15 +48,14 @@ jint jni_CSSNodeGetInstanceCount(alias_ref<jclass> clazz) {
jlong jni_CSSNodeNew(alias_ref<jobject> thiz) { jlong jni_CSSNodeNew(alias_ref<jobject> thiz) {
const CSSNodeRef node = CSSNodeNew(); const CSSNodeRef node = CSSNodeNew();
auto globalThiz = make_global(thiz); CSSNodeSetContext(node, Environment::current()->NewWeakGlobalRef(thiz.get()));
CSSNodeSetContext(node, globalThiz.release());
CSSNodeSetPrintFunc(node, _jniPrint); CSSNodeSetPrintFunc(node, _jniPrint);
return reinterpret_cast<jlong>(node); return reinterpret_cast<jlong>(node);
} }
void jni_CSSNodeFree(alias_ref<jobject> thiz, jlong nativePointer) { void jni_CSSNodeFree(alias_ref<jobject> thiz, jlong nativePointer) {
const auto globalContext = Environment::current()->DeleteWeakGlobalRef(
adopt_global(reinterpret_cast<jobject>(CSSNodeGetContext(_jlong2CSSNodeRef(nativePointer)))); reinterpret_cast<jweak>(CSSNodeGetContext(_jlong2CSSNodeRef(nativePointer))));
CSSNodeFree(_jlong2CSSNodeRef(nativePointer)); CSSNodeFree(_jlong2CSSNodeRef(nativePointer));
} }