From ba6992737457c21104a4da7bc581528f3206020c Mon Sep 17 00:00:00 2001 From: songhuan Date: Tue, 29 Apr 2025 18:24:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dxts=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E7=94=A8=E4=BE=8Bapp=20died?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id08e71a6e81cf4d78140e7031c84a5563b964829 Signed-off-by: songhuan --- frameworks/js/ani/include/sensor_ani.h | 7 +++++-- frameworks/js/ani/src/sensor_ani.cpp | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/frameworks/js/ani/include/sensor_ani.h b/frameworks/js/ani/include/sensor_ani.h index 3fd5c5a6..be11ac17 100644 --- a/frameworks/js/ani/include/sensor_ani.h +++ b/frameworks/js/ani/include/sensor_ani.h @@ -106,13 +106,14 @@ struct BusinessError { class AsyncCallbackInfo : public RefBase { public: - ani_env* env = nullptr; + ani_vm *vm = nullptr; + ani_env *env = nullptr; ani_ref callback[CALLBACK_NUM] = { 0 }; CallbackData data; BusinessError error; CallbackDataType type; vector sensorInfos; - AsyncCallbackInfo(ani_env* env, CallbackDataType type) : env(env), type(type) {} + AsyncCallbackInfo(ani_vm *vm, ani_env *env, CallbackDataType type) : vm(vm), env(env), type(type) {} ~AsyncCallbackInfo() { CALL_LOG_ENTER; @@ -122,6 +123,8 @@ public: SEN_HILOGD("Delete reference, i:%{public}d", i); env->GlobalReference_Delete(callback[i]); callback[i] = nullptr; + env = nullptr; + vm = nullptr; } } } diff --git a/frameworks/js/ani/src/sensor_ani.cpp b/frameworks/js/ani/src/sensor_ani.cpp index 9df28203..361c6e41 100644 --- a/frameworks/js/ani/src/sensor_ani.cpp +++ b/frameworks/js/ani/src/sensor_ani.cpp @@ -86,7 +86,7 @@ static float g_bodyState = -1.0f; static std::map>> g_subscribeCallbacks; static std::mutex onMutex_; static std::map>> g_onCallbackInfos; -static thread_local std::shared_ptr mainHandler; +static thread_local std::shared_ptr mainHandler = nullptr; static void ThrowBusinessError(ani_env *env, int errCode, std::string&& errMsg) { @@ -344,6 +344,16 @@ static void EmitUvEventLoop(sptr asyncCallbackInfo) CHKPV(asyncCallbackInfo); auto task = [asyncCallbackInfo]() { SEN_HILOGD("Begin to call task"); + ani_env *env = nullptr; + ani_options aniArgs {0, nullptr}; + if (ANI_ERROR == asyncCallbackInfo->vm->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env)) { + if (ANI_OK != asyncCallbackInfo->vm->GetEnv(ANI_VERSION_1, &env)) { + SEN_HILOGE("GetEnv failed"); + return; + } + } + asyncCallbackInfo->env = env; + AniLocalScopeGuard aniLocalScopeGuard(asyncCallbackInfo->env, ANI_SCOPE_SIZE); if (!aniLocalScopeGuard.IsStatusOK()) { SEN_HILOGE("CreateLocalScope failed"); @@ -452,12 +462,19 @@ static bool IsSubscribed(ani_env *env, int32_t sensorTypeId, ani_object callback } return false; } + static void UpdateCallbackInfos(ani_env *env, int32_t sensorTypeId, ani_object callback) { CALL_LOG_ENTER; std::lock_guard onCallbackLock(onMutex_); CHKCV((!IsSubscribed(env, sensorTypeId, callback)), "The callback has been subscribed"); - sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, ON_CALLBACK); + + ani_vm *vm = nullptr; + if (ANI_OK != env->GetVM(&vm)) { + SEN_HILOGE("GetVM failed."); + return; + } + sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(vm, env, ON_CALLBACK); CHKPV(asyncCallbackInfo); if (ANI_OK != env->GlobalReference_Create(callback, &asyncCallbackInfo->callback[0])) { -- Gitee