From 3c749f90da238722cfd14acae794559d871ee944 Mon Sep 17 00:00:00 2001 From: johnche Date: Thu, 26 Sep 2024 09:57:05 +0800 Subject: [PATCH] =?UTF-8?q?[unity]=E5=8A=A0=E4=B8=8AobjectInfo->ValueRef?= =?UTF-8?q?=E5=92=8CobjectInfo->EnvRef=E7=9A=84=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- unity/native_src_il2cpp/Src/PesapiV8Impl.cpp | 26 +++++++++++++++++--- unity/native_src_il2cpp/Src/Puerts.cpp | 3 +++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp b/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp index 82e41aad67..70f28a881d 100644 --- a/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp +++ b/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp @@ -36,13 +36,17 @@ struct pesapi_env_ref__ struct pesapi_value_ref__ { explicit pesapi_value_ref__(v8::Local context, v8::Local value) - : value_persistent(context->GetIsolate(), value), isolate(context->GetIsolate()), ref_count(1) + : value_persistent(context->GetIsolate(), value) + , isolate(context->GetIsolate()) + , ref_count(1) + , env_life_cycle_tracker(puerts::DataTransfer::GetJsEnvLifeCycleTracker(context->GetIsolate())) { } v8::Persistent value_persistent; v8::Isolate* const isolate; int ref_count; + std::weak_ptr env_life_cycle_tracker; }; struct pesapi_scope__ @@ -490,13 +494,15 @@ void pesapi_release_env_ref(pesapi_env_ref env_ref) { #if V8_MAJOR_VERSION < 11 env_ref->context_persistent.Empty(); + delete env_ref; +#else + ::operator delete(static_cast(env_ref)); #endif } else { - env_ref->context_persistent.Reset(); + delete env_ref; } - delete env_ref; } } @@ -575,7 +581,19 @@ void pesapi_release_value_ref(pesapi_value_ref value_ref) { if (--value_ref->ref_count == 0) { - delete value_ref; + if (value_ref->env_life_cycle_tracker.expired()) + { +#if V8_MAJOR_VERSION < 11 + value_ref->value_persistent.Empty(); + delete value_ref; +#else + ::operator delete(static_cast(value_ref)); +#endif + } + else + { + delete value_ref; + } } } diff --git a/unity/native_src_il2cpp/Src/Puerts.cpp b/unity/native_src_il2cpp/Src/Puerts.cpp index 791bffc2d6..0dacfe9a7f 100644 --- a/unity/native_src_il2cpp/Src/Puerts.cpp +++ b/unity/native_src_il2cpp/Src/Puerts.cpp @@ -269,6 +269,9 @@ static void UnrefJsObject(PObjectRefInfo* objectInfo) envInfo->PendingReleaseObjects.push_back(std::move(*obj)); } objectInfo->ExtraData = nullptr; + // 两个delete,可以通过直接用PObjectRefInfo placement new的方式优化,但需要p-api新增api + pesapi_release_value_ref(objectInfo->ValueRef); + pesapi_release_env_ref(objectInfo->EnvRef); } struct FieldWrapFuncInfo