Skip to content

Commit

Permalink
[unity]解决pesapi_create_function内存不回收的问题,原因见: #1945
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Dec 16, 2024
1 parent 668369e commit 09cfff3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions unity/native_src/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,16 @@ void FCppObjectMapper::CallbackDataGarbageCollected(const v8::WeakCallbackInfo<P

v8::MaybeLocal<v8::Function> FCppObjectMapper::CreateFunction(v8::Local<v8::Context> Context, pesapi_callback Callback, void* Data, pesapi_function_finalize Finalize)
{
auto Isolate = Context->GetIsolate();
auto CallbackData = new PesapiCallbackData {Callback, Data, this};
CallbackData->Finalize = Finalize;
auto V8Data = v8::External::New(Context->GetIsolate(), &CallbackData->Data);
auto Ret = v8::FunctionTemplate::New(Context->GetIsolate(), PesapiFunctionCallback, V8Data)->GetFunction(Context);
auto V8Data = v8::External::New(Isolate, &CallbackData->Data);
auto Template = v8::FunctionTemplate::New(Isolate, PesapiFunctionCallback, V8Data);
Template->Set(Isolate, "__do_not_cache", v8::ObjectTemplate::New(Isolate));
auto Ret = Template->GetFunction(Context);
if (!Ret.IsEmpty())
{
CallbackData->JsFunction.Reset(Context->GetIsolate(), Ret.ToLocalChecked());
CallbackData->JsFunction.Reset(Isolate, Ret.ToLocalChecked());
CallbackData->JsFunction.SetWeak<PesapiCallbackData>(
CallbackData, CallbackDataGarbageCollected, v8::WeakCallbackType::kInternalFields);
FunctionDatas.push_back(CallbackData);
Expand Down

0 comments on commit 09cfff3

Please sign in to comment.