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 committed Nov 26, 2024
1 parent 1a344af commit e68e48c
Showing 1 changed file with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,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 @@ -219,7 +220,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 @@ -234,7 +237,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 @@ -246,8 +251,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 @@ -263,8 +269,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 @@ -277,8 +284,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 @@ -288,22 +297,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 (!footstone::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 e68e48c

Please sign in to comment.