Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): java HippyMap leak oom by jni local ref #4135

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading