diff --git a/interfaces/plugin/include/async_callback_info.h b/interfaces/plugin/include/async_callback_info.h index 17d9c2a72fcbe2d04d236f037dc06eef7c4e5c05..fc2922bfce4a2bdd1e003dd153243a972d003f29 100644 --- a/interfaces/plugin/include/async_callback_info.h +++ b/interfaces/plugin/include/async_callback_info.h @@ -106,7 +106,6 @@ public: BusinessError error; CallbackDataType type; vector sensorInfos; - sptr callbackInfo = nullptr; AsyncCallbackInfo(napi_env env, CallbackDataType type) : env(env), type(type) {} ~AsyncCallbackInfo() diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 54960163a6d6c18b6c083f420b9530346fc8d81f..bbf2f46b231de93c9d10679ce86dc00e0ded8085 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -457,17 +457,20 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) napi_value resourceName = nullptr; CHKNRV(asyncCallbackInfo->env, napi_create_string_utf8(asyncCallbackInfo->env, "AsyncCallback", NAPI_AUTO_LENGTH, &resourceName), "napi_create_string_utf8"); - // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor - asyncCallbackInfo->callbackInfo = asyncCallbackInfo; + asyncCallbackInfo->IncStrongRef(nullptr); napi_status status = napi_create_async_work(asyncCallbackInfo->env, nullptr, resourceName, [](napi_env env, void* data) {}, [](napi_env env, napi_status status, void* data) { CALL_LOG_ENTER; - CHKPV(data); - sptr asyncCallbackInfo = reinterpret_cast(data)->callbackInfo; - CHKPV(asyncCallbackInfo); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->callbackInfo = nullptr; + sptr asyncCallbackInfo(static_cast(data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); napi_value callback = nullptr; napi_value callResult = nullptr; napi_value result[2] = {0}; @@ -494,14 +497,20 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) if (status != napi_ok || napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork) != napi_ok) { SEN_HILOGE("Create async work fail"); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->callbackInfo = nullptr; + asyncCallbackInfo->DecStrongRef(nullptr); } } -void EmitUvEventLoop(sptr asyncCallbackInfo) +void freeWork(uv_work_t *work) { CALL_LOG_ENTER; + CHKPV(work); + delete work; + work = nullptr; +} + +void EmitUvEventLoop(sptr asyncCallbackInfo) +{ CHKPV(asyncCallbackInfo); uv_loop_s *loop(nullptr); CHKNRV(asyncCallbackInfo->env, napi_get_uv_event_loop(asyncCallbackInfo->env, &loop), @@ -509,17 +518,20 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) CHKPV(loop); uv_work_t *work = new(std::nothrow) uv_work_t; CHKPV(work); - asyncCallbackInfo->callbackInfo = asyncCallbackInfo; asyncCallbackInfo->work = work; + asyncCallbackInfo->IncStrongRef(nullptr); work->data = asyncCallbackInfo.GetRefPtr(); int32_t ret = uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { - CALL_LOG_ENTER; CHKPV(work); - CHKPV(work->data); - sptr asyncCallbackInfo = - reinterpret_cast(work->data)->callbackInfo; - CHKPV(asyncCallbackInfo); - asyncCallbackInfo->callbackInfo = nullptr; + sptr asyncCallbackInfo(static_cast(work->data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); napi_env env = asyncCallbackInfo->env; napi_value callback = nullptr; CHKNRV(env, napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback), @@ -531,17 +543,13 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) g_convertfuncList[asyncCallbackInfo->type](env, asyncCallbackInfo, result); CHKNRV(env, napi_call_function(env, nullptr, callback, 1, &result[1], &callResult), "napi_call_function"); asyncCallbackInfo->work = nullptr; - CHKPV(work); - delete work; - work = nullptr; + freeWork(work); }); if (ret != 0) { SEN_HILOGE("uv_queue_work fail"); - asyncCallbackInfo->callbackInfo = nullptr; + asyncCallbackInfo->DecStrongRef(nullptr); asyncCallbackInfo->work = nullptr; - CHKPV(work); - delete work; - work = nullptr; + freeWork(work); } } @@ -552,17 +560,20 @@ void EmitPromiseWork(sptr asyncCallbackInfo) napi_value resourceName = nullptr; CHKNRV(asyncCallbackInfo->env, napi_create_string_latin1(asyncCallbackInfo->env, "Promise", NAPI_AUTO_LENGTH, &resourceName), "napi_create_string_latin1"); - // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor - asyncCallbackInfo->callbackInfo = asyncCallbackInfo; + asyncCallbackInfo->IncStrongRef(nullptr); napi_status status = napi_create_async_work(asyncCallbackInfo->env, nullptr, resourceName, [](napi_env env, void* data) {}, [](napi_env env, napi_status status, void* data) { CALL_LOG_ENTER; - CHKPV(data); - sptr asyncCallbackInfo = reinterpret_cast(data)->callbackInfo; - CHKPV(asyncCallbackInfo); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->callbackInfo = nullptr; + sptr asyncCallbackInfo(static_cast(data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); napi_value result[2] = {0}; CHKNCV(env, (g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end()), "Callback type invalid in promise"); @@ -580,8 +591,7 @@ void EmitPromiseWork(sptr asyncCallbackInfo) if (status != napi_ok || napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork) != napi_ok) { SEN_HILOGE("Create async work fail"); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->callbackInfo = nullptr; + asyncCallbackInfo->DecStrongRef(nullptr); } } } // namespace Sensors