Skip to content

Commit

Permalink
fix(android): java HippyMap leak oom by jni local ref
Browse files Browse the repository at this point in the history
  • Loading branch information
etkmao authored and siguangli committed Nov 28, 2024
1 parent 193cd78 commit ef9e0ef
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions android/sdk/src/main/jni/src/jni/convert_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ ConvertUtils::HandleObjectType(const std::shared_ptr<Ctx>& ctx,
j_env->DeleteLocalRef(method_name_str);
j_env->DeleteLocalRef(call_id_str);
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
return std::make_tuple(true, "", true);
Expand All @@ -210,7 +211,9 @@ ConvertUtils::HandleObjectType(const std::shared_ptr<Ctx>& ctx,
if (!std::get<0>(to_array_tuple)) {
return std::make_tuple(false, std::get<1>(to_array_tuple), false);
}
auto ref = std::make_shared<JavaRef>(j_env, std::get<2>(to_array_tuple));
auto j_obj = std::get<2>(to_array_tuple);
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
return std::make_tuple(true, "", true);
Expand All @@ -225,7 +228,9 @@ ConvertUtils::HandleObjectType(const std::shared_ptr<Ctx>& ctx,
if (!std::get<0>(to_map_tuple)) {
return std::make_tuple(false, std::get<1>(to_map_tuple), false);
}
auto ref = std::make_shared<JavaRef>(j_env, std::get<2>(to_map_tuple));
auto j_obj = std::get<2>(to_map_tuple);
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
return std::make_tuple(true, "", true);
Expand All @@ -237,8 +242,9 @@ ConvertUtils::HandleObjectType(const std::shared_ptr<Ctx>& ctx,
if (!ctx->GetValueBoolean(value, &b)) {
return std::make_tuple(false, "value must be boolean", false);
}
auto ref = std::make_shared<JavaRef>(j_env,
j_env->NewObject(boolean_clazz, boolean_constructor, b));
auto j_obj = j_env->NewObject(boolean_clazz, boolean_constructor, b);
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
return std::make_tuple(true, "", true);
Expand All @@ -253,8 +259,9 @@ ConvertUtils::HandleObjectType(const std::shared_ptr<Ctx>& ctx,
} else {
return std::make_tuple(false, "value must be string", false);
}

auto ref = std::make_shared<JavaRef>(j_env, j_env->NewStringUTF(str.c_str()));
auto j_obj = j_env->NewStringUTF(str.c_str());
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
return std::make_tuple(true, "", true);
Expand All @@ -267,8 +274,10 @@ ConvertUtils::HandleObjectType(const std::shared_ptr<Ctx>& ctx,
if (!ctx->GetValueNumber(value, &num)) {
return std::make_tuple(true, "value must be int", false);
}
auto ref = std::make_shared<JavaRef>(j_env, j_env->NewObject(
integer_clazz, integer_constructor, num));
auto j_obj = j_env->NewObject(
integer_clazz, integer_constructor, num);
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
} else {
Expand All @@ -278,22 +287,28 @@ ConvertUtils::HandleObjectType(const std::shared_ptr<Ctx>& ctx,
}

if (type == kDoubleObject) {
auto ref = std::make_shared<JavaRef>(j_env, j_env->NewObject(
double_clazz, double_constructor, num));
auto j_obj = j_env->NewObject(
double_clazz, double_constructor, num);
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
} else if (type == kFloatObject) {
auto ref = std::make_shared<JavaRef>(j_env, j_env->NewObject(
float_clazz, float_constructor, static_cast<float>(num)));
auto j_obj = j_env->NewObject(
float_clazz, float_constructor, static_cast<float>(num));
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
} else if (type == kLongObject) {
jlong jlong_value;
if (!hippy::base::numeric_cast<double, jlong>(num, jlong_value)) {
return std::make_tuple(true, "value out of jlong boundary", false);
}
auto ref = std::make_shared<JavaRef>(j_env, j_env->NewObject(
long_clazz, long_constructor, jlong_value));
auto j_obj = j_env->NewObject(
long_clazz, long_constructor, jlong_value);
auto ref = std::make_shared<JavaRef>(j_env, j_obj);
j_env->DeleteLocalRef(j_obj);
global_refs.push_back(ref);
j_args.l = ref->GetObj();
} else {
Expand Down

0 comments on commit ef9e0ef

Please sign in to comment.