From 964fd14390b9047cad6a9d004d604b49398b60dc Mon Sep 17 00:00:00 2001 From: etkmao Date: Tue, 26 Nov 2024 19:49:12 +0800 Subject: [PATCH] fix(android): java HippyMap leak oom by jni local ref --- .../js/src/main/cpp/src/convert_utils.cc | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/framework/android/connector/driver/js/src/main/cpp/src/convert_utils.cc b/framework/android/connector/driver/js/src/main/cpp/src/convert_utils.cc index 46615e04cf6..4a164ec0a31 100644 --- a/framework/android/connector/driver/js/src/main/cpp/src/convert_utils.cc +++ b/framework/android/connector/driver/js/src/main/cpp/src/convert_utils.cc @@ -205,6 +205,7 @@ ConvertUtils::HandleObjectType(const std::shared_ptr& ctx, j_env->DeleteLocalRef(method_name_str); j_env->DeleteLocalRef(call_id_str); auto ref = std::make_shared(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); @@ -219,7 +220,9 @@ ConvertUtils::HandleObjectType(const std::shared_ptr& 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(j_env, std::get<2>(to_array_tuple)); + auto j_obj = std::get<2>(to_array_tuple); + auto ref = std::make_shared(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); @@ -234,7 +237,9 @@ ConvertUtils::HandleObjectType(const std::shared_ptr& 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(j_env, std::get<2>(to_map_tuple)); + auto j_obj = std::get<2>(to_map_tuple); + auto ref = std::make_shared(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); @@ -246,8 +251,9 @@ ConvertUtils::HandleObjectType(const std::shared_ptr& ctx, if (!ctx->GetValueBoolean(value, &b)) { return std::make_tuple(false, "value must be boolean", false); } - auto ref = std::make_shared(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(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); @@ -263,8 +269,9 @@ ConvertUtils::HandleObjectType(const std::shared_ptr& ctx, } else { return std::make_tuple(false, "value must be string", false); } - - auto ref = std::make_shared(j_env, j_env->NewStringUTF(str.c_str())); + auto j_obj = j_env->NewStringUTF(str.c_str()); + auto ref = std::make_shared(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); @@ -277,8 +284,10 @@ ConvertUtils::HandleObjectType(const std::shared_ptr& ctx, if (!ctx->GetValueNumber(value, &num)) { return std::make_tuple(true, "value must be int", false); } - auto ref = std::make_shared(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(j_env, j_obj); + j_env->DeleteLocalRef(j_obj); global_refs.push_back(ref); j_args.l = ref->GetObj(); } else { @@ -288,13 +297,17 @@ ConvertUtils::HandleObjectType(const std::shared_ptr& ctx, } if (type == kDoubleObject) { - auto ref = std::make_shared(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(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(j_env, j_env->NewObject( - float_clazz, float_constructor, static_cast(num))); + auto j_obj = j_env->NewObject( + float_clazz, float_constructor, static_cast(num)); + auto ref = std::make_shared(j_env, j_obj); + j_env->DeleteLocalRef(j_obj); global_refs.push_back(ref); j_args.l = ref->GetObj(); } else if (type == kLongObject) { @@ -302,8 +315,10 @@ ConvertUtils::HandleObjectType(const std::shared_ptr& ctx, if (!footstone::numeric_cast(num, jlong_value)) { return std::make_tuple(true, "value out of jlong boundary", false); } - auto ref = std::make_shared(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(j_env, j_obj); + j_env->DeleteLocalRef(j_obj); global_refs.push_back(ref); j_args.l = ref->GetObj(); } else {