Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): fix memory issue when calling JSTurboObjectWithName #3777

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions framework/ios/base/executors/HippyJSExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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();
Expand All @@ -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::FunctionWrapper>([](hippy::CallbackInfo& info, void* data) {
auto name = info[0];
if (!name) {
return;
}
HippyOCTurboModule *turbo = (__bridge HippyOCTurboModule*) data;
auto turbo_wrapper = std::make_unique<TurboWrapper>(turbo, info[0]);
auto func_wrapper = std::make_unique<hippy::FunctionWrapper>([](hippy::CallbackInfo& info, void* data) {
std::vector<std::shared_ptr<hippy::CtxValue>> 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<TurboWrapper>(turbo, info[0]);
auto func_wrapper = std::make_unique<hippy::FunctionWrapper>([](hippy::CallbackInfo& info, void* data) {
std::vector<std::shared_ptr<hippy::CtxValue>> argv;
for (size_t i = 0; i < info.Length(); ++i) {
argv.push_back(info[i]);
}
auto scope_wrapper = reinterpret_cast<hippy::ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto turbo_wrapper = reinterpret_cast<TurboWrapper*>(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<hippy::ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto turbo_wrapper = reinterpret_cast<TurboWrapper*>(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<hippy::ScopeWrapper*>(std::any_cast<void*>(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);
Expand Down
Loading