diff --git a/driver/js/include/driver/scope.h b/driver/js/include/driver/scope.h index 860ced5de3e..826369d8f1b 100644 --- a/driver/js/include/driver/scope.h +++ b/driver/js/include/driver/scope.h @@ -119,6 +119,7 @@ struct ClassTemplate { string_view name; size_t size = SIZE_OF; std::unordered_map> holder_map; + std::vector> holder_ctx_values; }; class Scope : public std::enable_shared_from_this { diff --git a/driver/js/src/engine.cc b/driver/js/src/engine.cc index 7b64b9aa8c2..b79528673fe 100644 --- a/driver/js/src/engine.cc +++ b/driver/js/src/engine.cc @@ -44,7 +44,12 @@ Engine::Engine() Engine::~Engine() { FOOTSTONE_DLOG(INFO) << "~Engine"; - class_template_holder_map_.clear(); + for(auto& [key, template_map] : class_template_holder_map_) { + auto animation_template = std::any_cast>>(template_map["Animation"]); + animation_template->holder_ctx_values.clear(); + auto animation_set_template = std::any_cast>>(template_map["AnimationSet"]); + animation_set_template->holder_ctx_values.clear(); + } } void Engine::AsyncInitialize(std::shared_ptr js, diff --git a/driver/js/src/modules/animation_module.cc b/driver/js/src/modules/animation_module.cc index 8a63cc4ab1b..7146ffa178c 100644 --- a/driver/js/src/modules/animation_module.cc +++ b/driver/js/src/modules/animation_module.cc @@ -537,16 +537,22 @@ RegisterAnimation(const std::weak_ptr& weak_scope) { exception = context->CreateException("cb is not a function"); return nullptr; } - auto cb = [weak_scope, func] { // run in js thread + std::weak_ptr weak_func = func; + auto cb = [weak_scope, weak_func] { // run in js thread auto scope = weak_scope.lock(); if (!scope) { return; } auto context = scope->GetContext(); - context->CallFunction(func, context->GetGlobalObject(), 0, nullptr); + auto func = weak_func.lock(); + if (func) { + context->CallFunction(func, context->GetGlobalObject(), 0, nullptr); + } }; animation->AddEventListener(StringViewUtils::ToStdString(StringViewUtils::ConvertEncoding( event_name, string_view::Encoding::Utf8).utf8_value()), std::move(cb)); + auto class_template_ptr = std::any_cast>>(scope->GetClassTemplate("Animation")); + class_template_ptr->holder_ctx_values.emplace_back(func); return nullptr; }; class_template.functions.emplace_back(std::move(add_event_listener_func_def)); @@ -809,16 +815,22 @@ RegisterAnimationSet(const std::weak_ptr& weak_scope) { exception = context->CreateException("cb is not a function"); return nullptr; } - auto cb = [weak_scope, func] { + std::weak_ptr weak_func = func; + auto cb = [weak_scope, weak_func] { auto scope = weak_scope.lock(); if (!scope) { return; } auto context = scope->GetContext(); - context->CallFunction(func, context->GetGlobalObject(), 0, nullptr); + auto func = weak_func.lock(); + if (func) { + context->CallFunction(func, context->GetGlobalObject(), 0, nullptr); + } }; animation_set->AddEventListener(StringViewUtils::ToStdString(StringViewUtils::ConvertEncoding( event_name, string_view::Encoding::Utf8).utf8_value()), std::move(cb)); + auto class_template_ptr = std::any_cast>>(scope->GetClassTemplate("AnimationSet")); + class_template_ptr->holder_ctx_values.emplace_back(func); return nullptr; }; def.functions.emplace_back(std::move(add_event_listener_func_def));