From 9c373d2b7b5f9bfa9f500f587b4fed1a1bf07268 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Fri, 8 Mar 2024 16:21:45 +0800 Subject: [PATCH] fix(ios): fix memory issue when calling JSTurboObjectWithName --- .../ios/base/executors/HippyJSExecutor.mm | 60 +++++++++++-------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/framework/ios/base/executors/HippyJSExecutor.mm b/framework/ios/base/executors/HippyJSExecutor.mm index bf578268d10..260bbc6b4c9 100644 --- a/framework/ios/base/executors/HippyJSExecutor.mm +++ b/framework/ios/base/executors/HippyJSExecutor.mm @@ -193,6 +193,9 @@ - (void)setup { @autoreleasepool { //todo HippyJSExecutor *strongSelf = (__bridge HippyJSExecutor*)data; + if (!strongSelf) { + return; + } const auto &context = strongSelf.pScope->GetContext(); if (context->IsString(info[0])) { NSString *name = ObjectFromCtxValue(context, info[0]); @@ -275,7 +278,7 @@ - (void)setSandboxDirectory:(NSString *)directory { } - (SharedCtxValuePtr)JSTurboObjectWithName:(NSString *)name { - //create HostObject by name + // create HostObject by name HippyOCTurboModule *turboModule = [self->_bridge turboModuleWithName:name]; auto scope = self->_pScope; auto context = scope->GetContext(); @@ -286,37 +289,42 @@ - (SharedCtxValuePtr)JSTurboObjectWithName:(NSString *)name { // create jsProxy std::string turbo_name([name UTF8String]); if (scope->HasTurboInstance(turbo_name)) { - return scope->GetTurboInstance(turbo_name); + return scope->GetTurboInstance(turbo_name); } + + CFTypeRef retainedTurboModule = CFBridgingRetain(turboModule); auto wrapper = std::make_unique([](hippy::CallbackInfo& info, void* data) { - auto name = info[0]; - if (!name) { - return; - } - HippyOCTurboModule *turbo = (__bridge HippyOCTurboModule*) data; - auto turbo_wrapper = std::make_unique(turbo, info[0]); - auto func_wrapper = std::make_unique([](hippy::CallbackInfo& info, void* data) { - std::vector> argv; - for (size_t i = 0; i < info.Length(); ++i) { - argv.push_back(info[i]); + auto name = info[0]; + if (!name) { + CFRelease(data); + return; } + HippyOCTurboModule *turbo = (__bridge HippyOCTurboModule*)data; + auto turbo_wrapper = std::make_unique(turbo, info[0]); + auto func_wrapper = std::make_unique([](hippy::CallbackInfo& info, void* data) { + std::vector> argv; + for (size_t i = 0; i < info.Length(); ++i) { + argv.push_back(info[i]); + } + auto scope_wrapper = reinterpret_cast(std::any_cast(info.GetSlot())); + auto scope = scope_wrapper->scope.lock(); + FOOTSTONE_CHECK(scope); + auto turbo_wrapper = reinterpret_cast(data); + HippyOCTurboModule *turbo = turbo_wrapper->module; + auto name = turbo_wrapper->name; + auto result = [turbo invokeOCMethod:scope->GetContext() this_val:name args:argv.data() count:argv.size()]; + info.GetReturnValue()->Set(result); + }, turbo_wrapper.get()); + [turbo saveTurboWrapper:name turbo:std::move(turbo_wrapper)]; auto scope_wrapper = reinterpret_cast(std::any_cast(info.GetSlot())); auto scope = scope_wrapper->scope.lock(); FOOTSTONE_CHECK(scope); - auto turbo_wrapper = reinterpret_cast(data); - HippyOCTurboModule *turbo = turbo_wrapper->module; - auto name = turbo_wrapper->name; - auto result = [turbo invokeOCMethod:scope->GetContext() this_val:name args:argv.data() count:argv.size()]; - info.GetReturnValue()->Set(result); - }, turbo_wrapper.get()); - [turbo saveTurboWrapper:name turbo:std::move(turbo_wrapper)]; - auto scope_wrapper = reinterpret_cast(std::any_cast(info.GetSlot())); - auto scope = scope_wrapper->scope.lock(); - FOOTSTONE_CHECK(scope); - auto func = scope->GetContext()->CreateFunction(func_wrapper); - scope->SaveFunctionWrapper(std::move(func_wrapper)); - info.GetReturnValue()->Set(func); - }, (__bridge void*)turboModule); + auto func = scope->GetContext()->CreateFunction(func_wrapper); + scope->SaveFunctionWrapper(std::move(func_wrapper)); + info.GetReturnValue()->Set(func); + CFRelease(data); + }, (void *)retainedTurboModule); + auto obj = scope->GetContext()->DefineProxy(wrapper); scope->SaveFunctionWrapper(std::move(wrapper)); scope->SetTurboInstance(turbo_name, obj);