Skip to content

Commit

Permalink
[unity]加上objectInfo->ValueRef和objectInfo->EnvRef的释放
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Sep 26, 2024
1 parent 6f90d5f commit 3c749f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
26 changes: 22 additions & 4 deletions unity/native_src_il2cpp/Src/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ struct pesapi_env_ref__
struct pesapi_value_ref__
{
explicit pesapi_value_ref__(v8::Local<v8::Context> context, v8::Local<v8::Value> 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<v8::Value> value_persistent;
v8::Isolate* const isolate;
int ref_count;
std::weak_ptr<int> env_life_cycle_tracker;
};

struct pesapi_scope__
Expand Down Expand Up @@ -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<void*>(env_ref));
#endif
}
else
{
env_ref->context_persistent.Reset();
delete env_ref;
}
delete env_ref;
}
}

Expand Down Expand Up @@ -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<void*>(value_ref));
#endif
}
else
{
delete value_ref;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions unity/native_src_il2cpp/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c749f9

Please sign in to comment.