From b7595689703bab180f0a435161d3ec4888791d9e Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Fri, 20 Jan 2023 15:31:26 +0800 Subject: [PATCH 01/12] fix memory leak Signed-off-by: zhaolinglan --- .../js_keyboard_delegate_setting.cpp | 195 ++++++------------ .../js_keyboard_delegate_setting.h | 6 +- .../js/napi/inputmethodclient/async_call.h | 9 +- .../js/napi/inputmethodclient/js_utils.cpp | 15 +- 4 files changed, 77 insertions(+), 148 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 63c8df967..bd6956884 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -358,25 +358,28 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) KeyEventPara para{ keyCode, keyStatus, false }; std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp"); auto isDone = std::make_shared>(MAX_TIMEOUT, false); - uv_work_t *work = GetKeyEventUVwork(type, para, isDone); + auto work = GetUVwork(type, [para, isDone](UvEntry &entry) { + entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent }; + entry.isDone = isDone; + }); if (work == nullptr) { - IMSA_HILOGE("GetKeyEventUVwork nullptr"); + IMSA_HILOGE("failed to get uv work"); return false; } uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); auto getKeyEventProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } - napi_value jsObject = GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode, - entry->keyEventPara.keyStatus); + napi_value jsObject = + GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode, entry->keyEventPara.keyStatus); if (jsObject == nullptr) { IMSA_HILOGE("get GetResultOnKeyEvent failed: jsObject is nullptr"); return false; @@ -390,129 +393,18 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) return isDone->GetValue(); } -uv_work_t *JsKeyboardDelegateSetting::GetKeyEventUVwork( - std::string type, KeyEventPara para, std::shared_ptr> &isDone) -{ - IMSA_HILOGI("run in GetKeyEventUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent }; - entry->isDone = isDone; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - delete entry; - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsKeyboardDelegateSetting::GetCursorUVwork(std::string type, CursorPara para) -{ - IMSA_HILOGI("run in GetCursorUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->curPara.positionX = para.positionX; - entry->curPara.positionY = para.positionY; - entry->curPara.height = para.height; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsKeyboardDelegateSetting::GetSelectionUVwork(std::string type, SelectionPara para) -{ - IMSA_HILOGI("run in GetSelectionUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->selPara.oldBegin = para.oldBegin; - entry->selPara.oldEnd = para.oldEnd; - entry->selPara.newBegin = para.newBegin; - entry->selPara.newEnd = para.newEnd; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsKeyboardDelegateSetting::GetTextUVwork(std::string type, std::string text) -{ - IMSA_HILOGI("run in GetTextUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->text = text; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) { IMSA_HILOGI("run in OnCursorUpdate"); CursorPara para{ positionX, positionY, height }; std::string type = "cursorContextChange"; - uv_work_t *work = GetCursorUVwork(type, para); + auto work = GetUVwork(type, [para](UvEntry &entry) { + entry.curPara.positionX = para.positionX; + entry.curPara.positionY = para.positionY; + entry.curPara.height = para.height; + }); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( @@ -522,10 +414,9 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi delete data; delete work; }); - auto getCursorUpdateProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { - if (argc < 3) { + std::shared_ptr item) -> bool { + if (argc < ARGC_THREE) { return false; } napi_create_int32(item->env_, entry->curPara.positionX, &args[ARGC_ZERO]); @@ -533,7 +424,7 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi napi_create_int32(item->env_, entry->curPara.height, &args[ARGC_TWO]); return true; }; - JsUtils::TraverseCallback(entry->vecCopy, ARGC_THREE, getCursorUpdateProperty); + JsUtils::CallJsFunction(entry->vecCopy, ARGC_THREE, getCursorUpdateProperty); }); } @@ -542,8 +433,14 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE IMSA_HILOGI("run in OnSelectionChange"); SelectionPara para{ oldBegin, oldEnd, newBegin, newEnd }; std::string type = "selectionChange"; - uv_work_t *work = GetSelectionUVwork(type, para); + auto work = GetUVwork(type, [para](UvEntry &entry) { + entry.selPara.oldBegin = para.oldBegin; + entry.selPara.oldEnd = para.oldEnd; + entry.selPara.newBegin = para.newBegin; + entry.selPara.newEnd = para.newEnd; + }); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( @@ -555,7 +452,7 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE }); auto getSelectionChangeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc < 4) { return false; } @@ -565,7 +462,7 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE napi_create_int32(item->env_, entry->selPara.newEnd, &args[ARGC_THREE]); return true; }; - JsUtils::TraverseCallback(entry->vecCopy, ARGC_FOUR, getSelectionChangeProperty); + JsUtils::CallJsFunction(entry->vecCopy, ARGC_FOUR, getSelectionChangeProperty); }); } @@ -573,8 +470,9 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) { IMSA_HILOGI("run in OnTextChange"); std::string type = "cursorContextChange"; - uv_work_t *work = GetTextUVwork(type, text); + auto work = GetUVwork(type, [text](UvEntry &entry) { entry.text = text; }); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( @@ -586,15 +484,42 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) }); auto getTextChangeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { - if (argc == 0) { + std::shared_ptr item) -> bool { + if (argc < ARGC_ONE) { return false; } napi_create_string_utf8(item->env_, entry->text.c_str(), NAPI_AUTO_LENGTH, &args[ARGC_ZERO]); return true; }; - JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getTextChangeProperty); + JsUtils::CallJsFunction(entry->vecCopy, ARGC_ONE, getTextChangeProperty); }); } + +uv_work_t *JsKeyboardDelegateSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) +{ + IMSA_HILOGI("run in %{public}s", __func__); + UvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + + if (jsCbMap_[type].empty()) { + IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); + return nullptr; + } + entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); + if (entry == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + entrySetter(*entry); + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + work->data = entry; + return work; +} } // namespace MiscServices } // namespace OHOS diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h index 8d73dd78e..5241e904e 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h @@ -130,10 +130,8 @@ private: { } }; - uv_work_t *GetCursorUVwork(std::string type, CursorPara para); - uv_work_t *GetSelectionUVwork(std::string type, SelectionPara para); - uv_work_t *GetTextUVwork(std::string type, std::string text); - uv_work_t *GetKeyEventUVwork(std::string type, KeyEventPara para, std::shared_ptr> &isDone); + using EntrySetter = std::function; + uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter); uv_loop_s *loop_ = nullptr; std::recursive_mutex mutex_; std::map>> jsCbMap_; diff --git a/interfaces/kits/js/napi/inputmethodclient/async_call.h b/interfaces/kits/js/napi/inputmethodclient/async_call.h index d61728cbc..a91f6bbf9 100644 --- a/interfaces/kits/js/napi/inputmethodclient/async_call.h +++ b/interfaces/kits/js/napi/inputmethodclient/async_call.h @@ -58,7 +58,9 @@ public: if (input_ == nullptr) { return napi_ok; } - return input_(env, argc, argv, self); + auto ret = input_(env, argc, argv, self); + input_ = nullptr; + return ret; } virtual napi_status operator()(napi_env env, napi_value *result) @@ -67,7 +69,9 @@ public: *result = nullptr; return napi_ok; } - return output_(env, result); + auto ret = output_(env, result); + output_ = nullptr; + return ret; } virtual void Exec() @@ -76,6 +80,7 @@ public: return; } exec_(this); + exec_ = nullptr; }; protected: diff --git a/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp b/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp index 48c799106..8661a20f8 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp @@ -161,21 +161,23 @@ const std::string JsUtils::ToMessage(int32_t code) return "error is out of definition."; } -bool JsUtils::TraverseCallback(std::vector> &vecCopy, size_t paramNum, - ArgsProvider argsProvider) +bool JsUtils::TraverseCallback( + std::vector> &vecCopy, size_t paramNum, ArgsProvider argsProvider) { bool isResult = false; bool isOnKeyEvent = false; for (const auto &item : vecCopy) { + napi_handle_scope scope = nullptr; + napi_open_handle_scope(item->env_, &scope); if (item->threadId_ != std::this_thread::get_id()) { + napi_close_handle_scope(item->env_, scope); continue; } - napi_value args[MAX_ARGMENT_COUNT]; if (!argsProvider(args, MAX_ARGMENT_COUNT, item)) { + napi_close_handle_scope(item->env_, scope); continue; } - napi_value callback = nullptr; napi_value global = nullptr; napi_value result = nullptr; @@ -185,12 +187,10 @@ bool JsUtils::TraverseCallback(std::vector> &v napi_get_global(item->env_, &global); napi_status callStatus = napi_call_function(item->env_, global, callback, paramNum, args, &result); if (callStatus != napi_ok) { - IMSA_HILOGE( - "notify data change failed callStatus:%{public}d", callStatus); + IMSA_HILOGE("notify data change failed callStatus:%{public}d", callStatus); result = nullptr; } } - if (result != nullptr && !isOnKeyEvent) { napi_valuetype valueType = napi_undefined; napi_typeof(item->env_, result, &valueType); @@ -202,6 +202,7 @@ bool JsUtils::TraverseCallback(std::vector> &v isOnKeyEvent = true; } } + napi_close_handle_scope(item->env_, scope); } return isOnKeyEvent; } -- Gitee From 732c511f89fb616ae12b37543da36b04c9ab6854 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Fri, 20 Jan 2023 15:39:40 +0800 Subject: [PATCH 02/12] fix memory leak Signed-off-by: zhaolinglan --- .../js_keyboard_delegate_setting.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index bd6956884..8e07ecd42 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -416,7 +416,7 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi }); auto getCursorUpdateProperty = [entry](napi_value *args, uint8_t argc, std::shared_ptr item) -> bool { - if (argc < ARGC_THREE) { + if (argc < 3) { return false; } napi_create_int32(item->env_, entry->curPara.positionX, &args[ARGC_ZERO]); @@ -424,7 +424,7 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi napi_create_int32(item->env_, entry->curPara.height, &args[ARGC_TWO]); return true; }; - JsUtils::CallJsFunction(entry->vecCopy, ARGC_THREE, getCursorUpdateProperty); + JsUtils::TraverseCallback(entry->vecCopy, ARGC_THREE, getCursorUpdateProperty); }); } @@ -433,7 +433,7 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE IMSA_HILOGI("run in OnSelectionChange"); SelectionPara para{ oldBegin, oldEnd, newBegin, newEnd }; std::string type = "selectionChange"; - auto work = GetUVwork(type, [para](UvEntry &entry) { + auto work = GetUVwork(type, [¶](UvEntry &entry) { entry.selPara.oldBegin = para.oldBegin; entry.selPara.oldEnd = para.oldEnd; entry.selPara.newBegin = para.newBegin; @@ -462,7 +462,7 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE napi_create_int32(item->env_, entry->selPara.newEnd, &args[ARGC_THREE]); return true; }; - JsUtils::CallJsFunction(entry->vecCopy, ARGC_FOUR, getSelectionChangeProperty); + JsUtils::TraverseCallback(entry->vecCopy, ARGC_FOUR, getSelectionChangeProperty); }); } @@ -470,7 +470,7 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) { IMSA_HILOGI("run in OnTextChange"); std::string type = "cursorContextChange"; - auto work = GetUVwork(type, [text](UvEntry &entry) { entry.text = text; }); + auto work = GetUVwork(type, [&text](UvEntry &entry) { entry.text = text; }); if (work == nullptr) { IMSA_HILOGE("failed to get uv entry"); return; @@ -484,14 +484,14 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) }); auto getTextChangeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { - if (argc < ARGC_ONE) { + std::shared_ptr item) -> bool { + if (argc == 0) { return false; } napi_create_string_utf8(item->env_, entry->text.c_str(), NAPI_AUTO_LENGTH, &args[ARGC_ZERO]); return true; }; - JsUtils::CallJsFunction(entry->vecCopy, ARGC_ONE, getTextChangeProperty); + JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getTextChangeProperty); }); } -- Gitee From 90e53ebfaa024741a09f3e2303fc5187b4d503a4 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 28 Jan 2023 10:42:22 +0800 Subject: [PATCH 03/12] fix memory leak Signed-off-by: zhaolinglan --- .../js_input_method_engine_setting.cpp | 179 ++++++------------ .../js_input_method_engine_setting.h | 2 + .../js_keyboard_delegate_setting.cpp | 19 +- .../js_keyboard_delegate_setting.h | 2 +- .../js_get_input_method_setting.cpp | 66 ++++--- .../js_get_input_method_setting.h | 2 + 6 files changed, 104 insertions(+), 166 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp index de076ed7f..7a8da7979 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -384,113 +384,6 @@ bool JsInputMethodEngineSetting::Equals(napi_env env, napi_value value, napi_ref return isEquals; } -uv_work_t *JsInputMethodEngineSetting::GetUVwork(std::string type) -{ - IMSA_HILOGI("run in GetUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsInputMethodEngineSetting::GetStopInputUVwork(std::string type, std::string imeId) -{ - IMSA_HILOGI("run in GetStopInputUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->imeid = imeId; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsInputMethodEngineSetting::GetWindowIDUVwork(std::string type, uint32_t windowid) -{ - IMSA_HILOGI("run in GetWindowIDUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->windowid = windowid; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsInputMethodEngineSetting::GetSubtypeUVwork(std::string type, const SubProperty &property) -{ - IMSA_HILOGI("run in GetSubtypeUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->subProperty = property; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const SubProperty &property) { napi_value subType = nullptr; @@ -537,18 +430,19 @@ napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const void JsInputMethodEngineSetting::OnInputStart() { - IMSA_HILOGI("run in OnInputStart"); + IMSA_HILOGI("run in %{public}s", __func__); std::string type = "inputStart"; - uv_work_t *work = GetUVwork(type); + auto work = GetUVwork(type); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); if (entry == nullptr) { IMSA_HILOGE("OnInputStart:: entryptr is null"); @@ -577,17 +471,18 @@ void JsInputMethodEngineSetting::OnInputStart() void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) { std::string type = isShow ? "keyboardShow" : "keyboardHide"; - IMSA_HILOGI("run in OnKeyboardStatus: %{public}s", type.c_str()); - uv_work_t *work = GetUVwork(type); + IMSA_HILOGI("run in %{public}s: %{public}s",__func__, type.c_str()); + auto work = GetUVwork(type); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); auto getKeyboardStatusProperty = [](napi_value *args, uint8_t argc, @@ -606,16 +501,17 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) { IMSA_HILOGI("run in OnInputStop"); std::string type = "inputStop"; - uv_work_t *work = GetStopInputUVwork(type, imeId); + auto work = GetUVwork(type, [&imeId](UvEntry &entry) { entry.imeid = imeId; }); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); if (entry == nullptr) { IMSA_HILOGE("OnInputStop:: entryptr is null"); @@ -636,18 +532,19 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) { - IMSA_HILOGI("run in OnSetCallingWindow"); + IMSA_HILOGI("run in %{public}s", __func__); std::string type = "setCallingWindow"; - uv_work_t *work = GetWindowIDUVwork(type, windowId); + auto work = GetUVwork(type, [&windowId](UvEntry &entry) { entry.windowid = windowId; }); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); if (entry == nullptr) { IMSA_HILOGE("setCallingWindow:: entryptr is null"); @@ -668,18 +565,19 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) { - IMSA_HILOGI("run in OnSetSubtype"); + IMSA_HILOGI("run in %{public}s", __func__); std::string type = "setSubtype"; - uv_work_t *work = GetSubtypeUVwork(type, property); + auto work = GetUVwork(type, [&property](UvEntry &entry) { entry.subProperty = property; }); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); if (entry == nullptr) { IMSA_HILOGE("OnSetSubtype:: entryptr is null"); @@ -702,5 +600,34 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getSubtypeProperty); }); } + +uv_work_t *JsInputMethodEngineSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) +{ + IMSA_HILOGD("run in %{public}s: %{public}s", __func__, type.c_str()); + UvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + + if (jsCbMap_[type].empty()) { + IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); + return nullptr; + } + entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); + if (entry == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + if (entrySetter != nullptr) { + entrySetter(*entry); + } + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + work->data = entry; + return work; +} } // namespace MiscServices } // namespace OHOS diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h index e42830389..35a4fd7d4 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h @@ -76,6 +76,8 @@ private: { } }; + using EntrySetter = std::function; + uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); uv_loop_s *loop_ = nullptr; std::recursive_mutex mutex_; std::map>> jsCbMap_; diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 8e07ecd42..4a753feab 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -411,9 +411,10 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); + auto getCursorUpdateProperty = [entry](napi_value *args, uint8_t argc, std::shared_ptr item) -> bool { if (argc < 3) { @@ -447,8 +448,8 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); auto getSelectionChangeProperty = [entry](napi_value *args, uint8_t argc, @@ -479,8 +480,8 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); auto getTextChangeProperty = [entry](napi_value *args, uint8_t argc, @@ -497,7 +498,7 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) uv_work_t *JsKeyboardDelegateSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) { - IMSA_HILOGI("run in %{public}s", __func__); + IMSA_HILOGI("run in %{public}s: %{public}s", __func__, type.c_str()); UvEntry *entry = nullptr; { std::lock_guard lock(mutex_); @@ -511,7 +512,9 @@ uv_work_t *JsKeyboardDelegateSetting::GetUVwork(const std::string &type, EntrySe IMSA_HILOGE("entry ptr is nullptr!"); return nullptr; } - entrySetter(*entry); + if (entrySetter != nullptr) { + entrySetter(*entry); + } } uv_work_t *work = new (std::nothrow) uv_work_t; if (work == nullptr) { diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h index 5241e904e..e39f06977 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h @@ -131,7 +131,7 @@ private: } }; using EntrySetter = std::function; - uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter); + uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); uv_loop_s *loop_ = nullptr; std::recursive_mutex mutex_; std::map>> jsCbMap_; diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp index df5da23b7..d6be22338 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -542,41 +542,16 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info return result; } -uv_work_t *JsGetInputMethodSetting::GetImeChangeUVwork( - std::string type, const Property &property, const SubProperty &subProperty) -{ - IMSA_HILOGI("run in GetImeChangeUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->property = property; - entry->subProperty = subProperty; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubProperty &subProperty) { - IMSA_HILOGI("run in OnImeChange"); + IMSA_HILOGI("run in %{public}s", __func__); std::string type = "imeChange"; - uv_work_t *work = GetImeChangeUVwork(type, property, subProperty); + auto work = GetUVwork(type, [&property, &subProperty](UvEntry &entry) { + entry.property = property; + entry.subProperty = subProperty; + }); if (work == nullptr) { + IMSA_HILOGE("failed to get uv entry"); return; } uv_queue_work( @@ -608,5 +583,34 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro JsUtils::TraverseCallback(entry->vecCopy, ARGC_TWO, getImeChangeProperty); }); } + +uv_work_t *JsGetInputMethodSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) +{ + IMSA_HILOGD("run in %{public}s: %{public}s", __func__, type.c_str()); + UvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + + if (jsCbMap_[type].empty()) { + IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); + return nullptr; + } + entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); + if (entry == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + if (entrySetter != nullptr) { + entrySetter(*entry); + } + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + work->data = entry; + return work; +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h index 2b0ff9e5c..369244c5f 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -128,6 +128,8 @@ private: { } }; + using EntrySetter = std::function; + uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); static const std::string IMS_CLASS_NAME; static thread_local napi_ref IMSRef_; uv_loop_s *loop_ = nullptr; -- Gitee From 1f53d4309a86a050389504d6dd2db838ff8b3a71 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 28 Jan 2023 11:02:27 +0800 Subject: [PATCH 04/12] fix memory leak Signed-off-by: zhaolinglan --- .../js_input_method_engine_setting.h | 4 ---- .../js_keyboard_controller_engine.cpp | 17 ++++++++--------- .../js_text_input_client_engine.cpp | 17 ++++++++--------- .../js_get_input_method_controller.cpp | 19 +++++++++---------- .../js_get_input_method_setting.h | 1 - 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h index 35a4fd7d4..2ddda5380 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h @@ -57,10 +57,6 @@ private: static napi_value GetIMEInstance(napi_env env, napi_callback_info info, int flag); void RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); void UnRegisterListener(napi_value callback, std::string type); - uv_work_t *GetUVwork(std::string type); - uv_work_t *GetStopInputUVwork(std::string type, std::string imeId); - uv_work_t *GetWindowIDUVwork(std::string type, uint32_t windowid); - uv_work_t *GetSubtypeUVwork(std::string type, const SubProperty &property); static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property); static std::string GetStringProperty(napi_env env, napi_value jsString); static constexpr int32_t MAX_VALUE_LEN = 1024; diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp index fb9f43258..605f3f935 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp @@ -51,15 +51,14 @@ napi_value JsKeyboardControllerEngine::JsConstructor(napi_env env, napi_callback napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, controllerObject, - [](napi_env env, void *data, void *hint) { - auto *objInfo = reinterpret_cast(data); - if (objInfo != nullptr) { - delete objInfo; - } - }, - nullptr, nullptr); + auto finalize = [](napi_env env, void *data, void *hint) { + IMSA_HILOGD("JsKeyboardControllerEngine finalize"); + auto *objInfo = reinterpret_cast(data); + if (objInfo != nullptr) { + delete objInfo; + } + }; + napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp index a8f535239..c7362a186 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -93,15 +93,14 @@ napi_value JsTextInputClientEngine::JsConstructor(napi_env env, napi_callback_in napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, clientObject, - [](napi_env env, void *data, void *hint) { - auto *objInfo = reinterpret_cast(data); - if (objInfo != nullptr) { - delete objInfo; - } - }, - nullptr, nullptr); + auto finalize = [](napi_env env, void *data, void *hint) { + IMSA_HILOGD("JsTextInputClientEngine finalize"); + auto *objInfo = reinterpret_cast(data); + if (objInfo != nullptr) { + delete objInfo; + } + }; + napi_wrap(env, thisVar, clientObject, finalize, nullptr, nullptr); return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp index cec221bb1..69b253837 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -59,16 +59,15 @@ napi_value JsGetInputMethodController::JsConstructor(napi_env env, napi_callback napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, controllerObject, - [](napi_env env, void *data, void *hint) { - auto *objInfo = reinterpret_cast(data); - if (objInfo != nullptr) { - IMSA_HILOGE("objInfo is nullptr"); - delete objInfo; - } - }, - nullptr, nullptr); + auto finalize = [](napi_env env, void *data, void *hint) { + IMSA_HILOGD("JsGetInputMethodController finalize"); + auto *objInfo = reinterpret_cast(data); + if (objInfo != nullptr) { + IMSA_HILOGE("objInfo is nullptr"); + delete objInfo; + } + }; + napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h index 369244c5f..c661923d0 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -111,7 +111,6 @@ public: private: static napi_status GetInputMethodProperty(napi_env env, napi_value argv, std::shared_ptr ctxt); static JsGetInputMethodSetting *GetNative(napi_env env, napi_callback_info info); - uv_work_t *GetImeChangeUVwork(std::string type, const Property &property, const SubProperty &subProperty); static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); static napi_value GetJsConstProperty(napi_env env, uint32_t num); static napi_value DisplayInputMethod(napi_env env, napi_callback_info info, bool needThrowException); -- Gitee From cd03c8624e0f41a01af49d3922f05f1bcf06aec0 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 28 Jan 2023 11:10:49 +0800 Subject: [PATCH 05/12] fix memory leak Signed-off-by: zhaolinglan --- .../inputmethodability/js_input_method_engine_setting.cpp | 2 +- .../napi/inputmethodability/js_keyboard_delegate_setting.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp index 7a8da7979..792d72ab4 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -534,7 +534,7 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) { IMSA_HILOGI("run in %{public}s", __func__); std::string type = "setCallingWindow"; - auto work = GetUVwork(type, [&windowId](UvEntry &entry) { entry.windowid = windowId; }); + auto work = GetUVwork(type, [windowId](UvEntry &entry) { entry.windowid = windowId; }); if (work == nullptr) { IMSA_HILOGE("failed to get uv entry"); return; diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 4a753feab..fe7ae26bf 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -358,7 +358,7 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) KeyEventPara para{ keyCode, keyStatus, false }; std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp"); auto isDone = std::make_shared>(MAX_TIMEOUT, false); - auto work = GetUVwork(type, [para, isDone](UvEntry &entry) { + auto work = GetUVwork(type, [¶, isDone](UvEntry &entry) { entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent }; entry.isDone = isDone; }); @@ -398,7 +398,7 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi IMSA_HILOGI("run in OnCursorUpdate"); CursorPara para{ positionX, positionY, height }; std::string type = "cursorContextChange"; - auto work = GetUVwork(type, [para](UvEntry &entry) { + auto work = GetUVwork(type, [¶](UvEntry &entry) { entry.curPara.positionX = para.positionX; entry.curPara.positionY = para.positionY; entry.curPara.height = para.height; -- Gitee From e4645edc998dfa0fc8c6dc15c4e9f53044a5538c Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 28 Jan 2023 15:53:42 +0800 Subject: [PATCH 06/12] fix memory leak Signed-off-by: zhaolinglan --- .../inputmethodclient/js_get_input_method_setting.cpp | 4 ++-- interfaces/kits/js/napi/inputmethodclient/js_utils.h | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp index d6be22338..61710c9c5 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -558,8 +558,8 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; + SAFE_DELETE(data); + SAFE_DELETE(work); }); if (entry == nullptr) { IMSA_HILOGE("OnInputStart:: entryptr is null"); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_utils.h b/interfaces/kits/js/napi/inputmethodclient/js_utils.h index 623721ca3..d1fa39671 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_utils.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_utils.h @@ -82,6 +82,14 @@ private: static constexpr uint8_t MAX_ARGMENT_COUNT = 10; }; + +#define SAFE_DELETE(p) \ + do { \ + if ((p) != nullptr) { \ + delete (p); \ + (p) = nullptr; \ + } \ + } while (0) } // namespace MiscServices } // namespace OHOS #endif // INTERFACE_KITS_JS_UTILS_H \ No newline at end of file -- Gitee From ea49f3bea36426b0009e11d4d10b5736d4360ea4 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 28 Jan 2023 16:21:43 +0800 Subject: [PATCH 07/12] fix memory leak Signed-off-by: zhaolinglan --- .../js_input_method_engine_setting.cpp | 10 +++++----- .../js_keyboard_delegate_setting.cpp | 8 ++++---- .../inputmethodclient/js_get_input_method_setting.cpp | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp index 792d72ab4..b2d0a3cc1 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -440,7 +440,7 @@ void JsInputMethodEngineSetting::OnInputStart() uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); @@ -480,7 +480,7 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); @@ -509,7 +509,7 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); @@ -542,7 +542,7 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); @@ -575,7 +575,7 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index fe7ae26bf..19c17c2c7 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -369,7 +369,7 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); @@ -410,7 +410,7 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); @@ -447,7 +447,7 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); @@ -479,7 +479,7 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 61710c9c5..0f8f3dca3 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -557,7 +557,7 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { SAFE_DELETE(data); SAFE_DELETE(work); }); -- Gitee From e45a810eeb55738f37aa3c8002f1547465c54f6f Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 28 Jan 2023 20:33:49 +0800 Subject: [PATCH 08/12] fix memory leak Signed-off-by: zhaolinglan --- .../js_input_method_engine_setting.cpp | 10 ++++++---- .../js_keyboard_controller_engine.cpp | 9 +++++++-- .../js_keyboard_delegate_setting.cpp | 10 ++++++---- .../inputmethodability/js_text_input_client_engine.cpp | 10 +++++++--- .../js_get_input_method_controller.cpp | 9 +++++++-- .../inputmethodclient/js_get_input_method_setting.cpp | 9 ++++++--- 6 files changed, 39 insertions(+), 18 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp index b2d0a3cc1..e35d59ee6 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -133,10 +133,12 @@ napi_value JsInputMethodEngineSetting::JsConstructor(napi_env env, napi_callback napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, delegate.get(), - [](napi_env env, void *nativeObject, void *hint) { IMSA_HILOGE("delete JsInputMethodEngineSetting"); }, - nullptr, nullptr); + napi_status status = napi_wrap( + env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsInputMethodEngineSetting napi_wrap failed: %{public}d", status); + return nullptr; + } if (delegate->loop_ == nullptr) { napi_get_uv_event_loop(env, &delegate->loop_); } diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp index 605f3f935..ed5297411 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp @@ -52,13 +52,18 @@ napi_value JsKeyboardControllerEngine::JsConstructor(napi_env env, napi_callback return result; } auto finalize = [](napi_env env, void *data, void *hint) { - IMSA_HILOGD("JsKeyboardControllerEngine finalize"); + IMSA_HILOGE("JsKeyboardControllerEngine finalize"); auto *objInfo = reinterpret_cast(data); if (objInfo != nullptr) { delete objInfo; } }; - napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); + napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsKeyboardControllerEngine napi_wrap failed: %{public}d", status); + finalize(env, controllerObject, nullptr); + return nullptr; + } return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 19c17c2c7..b7bb529b3 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -113,10 +113,12 @@ napi_value JsKeyboardDelegateSetting::JsConstructor(napi_env env, napi_callback_ napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, delegate.get(), - [](napi_env env, void *nativeObject, void *hint) { IMSA_HILOGE("delete JsKeyboardDelegateSetting"); }, nullptr, - nullptr); + napi_status status = napi_wrap( + env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsKeyboardDelegateSetting napi_wrap failed: %{public}d", status); + return nullptr; + } if (delegate->loop_ == nullptr) { napi_get_uv_event_loop(env, &delegate->loop_); } diff --git a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp index c7362a186..269a306ef 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -94,14 +94,18 @@ napi_value JsTextInputClientEngine::JsConstructor(napi_env env, napi_callback_in return result; } auto finalize = [](napi_env env, void *data, void *hint) { - IMSA_HILOGD("JsTextInputClientEngine finalize"); + IMSA_HILOGE("JsTextInputClientEngine finalize"); auto *objInfo = reinterpret_cast(data); if (objInfo != nullptr) { delete objInfo; } }; - napi_wrap(env, thisVar, clientObject, finalize, nullptr, nullptr); - + napi_status status = napi_wrap(env, thisVar, clientObject, finalize, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsTextInputClientEngine napi_wrap failed: %{public}d", status); + finalize(env, clientObject, nullptr); + return nullptr; + } return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 69b253837..e2561444f 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -60,14 +60,19 @@ napi_value JsGetInputMethodController::JsConstructor(napi_env env, napi_callback return result; } auto finalize = [](napi_env env, void *data, void *hint) { - IMSA_HILOGD("JsGetInputMethodController finalize"); + IMSA_HILOGE("JsGetInputMethodController finalize"); auto *objInfo = reinterpret_cast(data); if (objInfo != nullptr) { IMSA_HILOGE("objInfo is nullptr"); delete objInfo; } }; - napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); + napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsGetInputMethodController napi_wrap failed:%{public}d", status); + finalize(env, controllerObject, nullptr); + return nullptr; + } return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 0f8f3dca3..7601711f5 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -104,9 +104,12 @@ napi_value JsGetInputMethodSetting::JsConstructor(napi_env env, napi_callback_in napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, delegate.get(), - [](napi_env env, void *data, void *hint) { IMSA_HILOGE("delete JsInputMethodSetting"); }, nullptr, nullptr); + napi_status status = napi_wrap( + env, thisVar, delegate.get(), [](napi_env env, void *data, void *hint) {}, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsGetInputMethodSetting napi_wrap failed: %{public}d", status); + return nullptr; + } if (delegate->loop_ == nullptr) { napi_get_uv_event_loop(env, &delegate->loop_); } -- Gitee From 9f22346a9a8ee1a606fc39910de638c0f7f37a9e Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Mon, 30 Jan 2023 20:57:41 +0800 Subject: [PATCH 09/12] fix memory leak Signed-off-by: zhaolinglan --- .../napi/inputmethodability/js_input_method_engine_setting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp index e35d59ee6..c31f8f858 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -473,7 +473,7 @@ void JsInputMethodEngineSetting::OnInputStart() void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) { std::string type = isShow ? "keyboardShow" : "keyboardHide"; - IMSA_HILOGI("run in %{public}s: %{public}s",__func__, type.c_str()); + IMSA_HILOGI("run in %{public}s: %{public}s", __func__, type.c_str()); auto work = GetUVwork(type); if (work == nullptr) { IMSA_HILOGE("failed to get uv entry"); -- Gitee From 60f9b9d641452757ee2f890613cab9c2f231c50b Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Thu, 2 Feb 2023 22:01:08 +0800 Subject: [PATCH 10/12] fix memory leak Signed-off-by: zhaolinglan --- .../inputmethodability/js_keyboard_controller_engine.h | 2 ++ .../napi/inputmethodability/js_text_input_client_engine.h | 8 ++++++++ .../inputmethodclient/js_get_input_method_controller.h | 1 + .../napi/inputmethodclient/js_get_input_method_setting.h | 3 +++ .../kits/js/napi/inputmethodclient/js_input_method.h | 1 + 5 files changed, 15 insertions(+) diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.h b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.h index b7c599f95..95e6aeefb 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.h +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.h @@ -33,6 +33,7 @@ struct HideKeyboardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -52,6 +53,7 @@ struct HideContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); diff --git a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.h b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.h index 2493b5cb7..f0fe9525a 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.h +++ b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.h @@ -38,6 +38,7 @@ struct SendKeyFunctionContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -58,6 +59,7 @@ struct MoveCursorContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -79,6 +81,7 @@ struct DeleteForwardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -100,6 +103,7 @@ struct DeleteBackwardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -121,6 +125,7 @@ struct InsertTextContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -142,6 +147,7 @@ struct GetForwardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -163,6 +169,7 @@ struct GetBackwardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -184,6 +191,7 @@ struct GetEditorAttributeContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h index 1b9e9c5ad..bd0f9e196 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -35,6 +35,7 @@ struct HandleContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h index c661923d0..eed93cc40 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -42,6 +42,7 @@ struct ListInputContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -63,6 +64,7 @@ struct DisplayOptionalInputMethodContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -84,6 +86,7 @@ struct GetInputMethodControllerContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_input_method.h b/interfaces/kits/js/napi/inputmethodclient/js_input_method.h index 55c84ca05..95def0c91 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_input_method.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_input_method.h @@ -41,6 +41,7 @@ struct SwitchInputMethodContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); -- Gitee From ece8f4ec5399122e97c06e4f8a8fcd9deaffa94b Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Thu, 2 Feb 2023 22:07:08 +0800 Subject: [PATCH 11/12] fix memory leak Signed-off-by: zhaolinglan --- .../js_input_method_engine_setting.cpp | 10 +++++----- .../js_keyboard_delegate_setting.cpp | 6 +++--- .../inputmethodclient/js_get_input_method_setting.cpp | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp index c31f8f858..4051819ee 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -436,7 +436,7 @@ void JsInputMethodEngineSetting::OnInputStart() std::string type = "inputStart"; auto work = GetUVwork(type); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -476,7 +476,7 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) IMSA_HILOGI("run in %{public}s: %{public}s", __func__, type.c_str()); auto work = GetUVwork(type); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -505,7 +505,7 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) std::string type = "inputStop"; auto work = GetUVwork(type, [&imeId](UvEntry &entry) { entry.imeid = imeId; }); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -538,7 +538,7 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) std::string type = "setCallingWindow"; auto work = GetUVwork(type, [windowId](UvEntry &entry) { entry.windowid = windowId; }); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -571,7 +571,7 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) std::string type = "setSubtype"; auto work = GetUVwork(type, [&property](UvEntry &entry) { entry.subProperty = property; }); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 92ad37ea3..119d57376 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -407,7 +407,7 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi entry.curPara.height = para.height; }); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -444,7 +444,7 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE entry.selPara.newEnd = para.newEnd; }); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -476,7 +476,7 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) std::string type = "textChange"; auto work = GetUVwork(type, [&text](UvEntry &entry) { entry.text = text; }); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp index dfbb47822..9a6436ab0 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -523,7 +523,7 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro entry.subProperty = subProperty; }); if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry"); + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( -- Gitee From a55e2dae5f1695440462549f0dfc0b05e393a8c0 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Fri, 3 Feb 2023 11:31:23 +0800 Subject: [PATCH 12/12] modify code Signed-off-by: zhaolinglan --- .../js_input_method_engine_setting.cpp | 64 +++++++++---------- .../js_keyboard_controller_engine.cpp | 2 +- .../js_keyboard_delegate_setting.cpp | 44 ++++++------- .../js_text_input_client_engine.cpp | 2 +- .../js_get_input_method_controller.cpp | 2 +- .../js_get_input_method_setting.cpp | 14 ++-- .../kits/js/napi/inputmethodclient/js_utils.h | 8 --- 7 files changed, 64 insertions(+), 72 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp index 4051819ee..8e0893ae4 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -432,9 +432,9 @@ napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const void JsInputMethodEngineSetting::OnInputStart() { - IMSA_HILOGI("run in %{public}s", __func__); + IMSA_HILOGD("run in"); std::string type = "inputStart"; - auto work = GetUVwork(type); + uv_work_t *work = GetUVwork(type); if (work == nullptr) { IMSA_HILOGD("failed to get uv entry"); return; @@ -442,9 +442,9 @@ void JsInputMethodEngineSetting::OnInputStart() uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); if (entry == nullptr) { IMSA_HILOGE("OnInputStart:: entryptr is null"); @@ -452,7 +452,7 @@ void JsInputMethodEngineSetting::OnInputStart() } auto getInputStartProperty = [](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc < 2) { return false; } @@ -473,8 +473,8 @@ void JsInputMethodEngineSetting::OnInputStart() void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) { std::string type = isShow ? "keyboardShow" : "keyboardHide"; - IMSA_HILOGI("run in %{public}s: %{public}s", __func__, type.c_str()); - auto work = GetUVwork(type); + IMSA_HILOGD("run in, %{public}s", type.c_str()); + uv_work_t *work = GetUVwork(type); if (work == nullptr) { IMSA_HILOGD("failed to get uv entry"); return; @@ -482,13 +482,13 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); auto getKeyboardStatusProperty = [](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -501,9 +501,9 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) { - IMSA_HILOGI("run in OnInputStop"); + IMSA_HILOGD("run in"); std::string type = "inputStop"; - auto work = GetUVwork(type, [&imeId](UvEntry &entry) { entry.imeid = imeId; }); + uv_work_t *work = GetUVwork(type, [&imeId](UvEntry &entry) { entry.imeid = imeId; }); if (work == nullptr) { IMSA_HILOGD("failed to get uv entry"); return; @@ -511,9 +511,9 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); if (entry == nullptr) { IMSA_HILOGE("OnInputStop:: entryptr is null"); @@ -521,7 +521,7 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) } auto getInputStopProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -534,9 +534,9 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) { - IMSA_HILOGI("run in %{public}s", __func__); + IMSA_HILOGD("run in"); std::string type = "setCallingWindow"; - auto work = GetUVwork(type, [windowId](UvEntry &entry) { entry.windowid = windowId; }); + uv_work_t *work = GetUVwork(type, [windowId](UvEntry &entry) { entry.windowid = windowId; }); if (work == nullptr) { IMSA_HILOGD("failed to get uv entry"); return; @@ -544,9 +544,9 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); if (entry == nullptr) { IMSA_HILOGE("setCallingWindow:: entryptr is null"); @@ -554,7 +554,7 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) } auto getCallingWindowProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -567,9 +567,9 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) { - IMSA_HILOGI("run in %{public}s", __func__); + IMSA_HILOGD("run in"); std::string type = "setSubtype"; - auto work = GetUVwork(type, [&property](UvEntry &entry) { entry.subProperty = property; }); + uv_work_t *work = GetUVwork(type, [&property](UvEntry &entry) { entry.subProperty = property; }); if (work == nullptr) { IMSA_HILOGD("failed to get uv entry"); return; @@ -577,9 +577,9 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); if (entry == nullptr) { IMSA_HILOGE("OnSetSubtype:: entryptr is null"); @@ -587,7 +587,7 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) } auto getSubtypeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -596,7 +596,7 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) IMSA_HILOGE("get GetResultOnSetSubtype failed: jsObject is nullptr"); return false; } - args[ARGC_ZERO] = {jsObject}; + args[ARGC_ZERO] = { jsObject }; return true; }; JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getSubtypeProperty); @@ -605,7 +605,7 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) uv_work_t *JsInputMethodEngineSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) { - IMSA_HILOGD("run in %{public}s: %{public}s", __func__, type.c_str()); + IMSA_HILOGD("run in, type: %{public}s", type.c_str()); UvEntry *entry = nullptr; { std::lock_guard lock(mutex_); diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp index ed5297411..a89522cf6 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp @@ -61,7 +61,7 @@ napi_value JsKeyboardControllerEngine::JsConstructor(napi_env env, napi_callback napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); if (status != napi_ok) { IMSA_HILOGE("JsKeyboardControllerEngine napi_wrap failed: %{public}d", status); - finalize(env, controllerObject, nullptr); + delete controllerObject; return nullptr; } diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 119d57376..5a50b01dd 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -357,11 +357,11 @@ napi_value JsKeyboardDelegateSetting::GetResultOnKeyEvent(napi_env env, int32_t bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) { - IMSA_HILOGI("run in OnKeyEvent"); + IMSA_HILOGD("run in"); KeyEventPara para{ keyCode, keyStatus, false }; std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp"); auto isDone = std::make_shared>(MAX_TIMEOUT, false); - auto work = GetUVwork(type, [¶, isDone](UvEntry &entry) { + uv_work_t *work = GetUVwork(type, [¶, isDone](UvEntry &entry) { entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent }; entry.isDone = isDone; }); @@ -372,9 +372,9 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); auto getKeyEventProperty = [entry](napi_value *args, uint8_t argc, std::shared_ptr item) -> bool { @@ -398,10 +398,10 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) { - IMSA_HILOGI("run in OnCursorUpdate"); + IMSA_HILOGD("run in"); CursorPara para{ positionX, positionY, height }; std::string type = "cursorContextChange"; - auto work = GetUVwork(type, [¶](UvEntry &entry) { + uv_work_t *work = GetUVwork(type, [¶](UvEntry &entry) { entry.curPara.positionX = para.positionX; entry.curPara.positionY = para.positionY; entry.curPara.height = para.height; @@ -413,9 +413,9 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); auto getCursorUpdateProperty = [entry](napi_value *args, uint8_t argc, @@ -434,10 +434,10 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { - IMSA_HILOGI("run in OnSelectionChange"); + IMSA_HILOGD("run in"); SelectionPara para{ oldBegin, oldEnd, newBegin, newEnd }; std::string type = "selectionChange"; - auto work = GetUVwork(type, [¶](UvEntry &entry) { + uv_work_t *work = GetUVwork(type, [¶](UvEntry &entry) { entry.selPara.oldBegin = para.oldBegin; entry.selPara.oldEnd = para.oldEnd; entry.selPara.newBegin = para.newBegin; @@ -450,9 +450,9 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); auto getSelectionChangeProperty = [entry](napi_value *args, uint8_t argc, @@ -472,9 +472,9 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) { - IMSA_HILOGI("run in OnTextChange"); + IMSA_HILOGD("run in"); std::string type = "textChange"; - auto work = GetUVwork(type, [&text](UvEntry &entry) { entry.text = text; }); + uv_work_t *work = GetUVwork(type, [&text](UvEntry &entry) { entry.text = text; }); if (work == nullptr) { IMSA_HILOGD("failed to get uv entry"); return; @@ -482,13 +482,13 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); auto getTextChangeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -501,7 +501,7 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) uv_work_t *JsKeyboardDelegateSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) { - IMSA_HILOGI("run in %{public}s: %{public}s", __func__, type.c_str()); + IMSA_HILOGD("run in, type: %{public}s", type.c_str()); UvEntry *entry = nullptr; { std::lock_guard lock(mutex_); diff --git a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp index 269a306ef..d6fc39844 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -103,7 +103,7 @@ napi_value JsTextInputClientEngine::JsConstructor(napi_env env, napi_callback_in napi_status status = napi_wrap(env, thisVar, clientObject, finalize, nullptr, nullptr); if (status != napi_ok) { IMSA_HILOGE("JsTextInputClientEngine napi_wrap failed: %{public}d", status); - finalize(env, clientObject, nullptr); + delete clientObject; return nullptr; } return thisVar; diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 455f48d5c..af866566b 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -70,7 +70,7 @@ napi_value JsGetInputMethodController::JsConstructor(napi_env env, napi_callback napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); if (status != napi_ok) { IMSA_HILOGE("JsGetInputMethodController napi_wrap failed:%{public}d", status); - finalize(env, controllerObject, nullptr); + delete controllerObject; return nullptr; } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 9a6436ab0..260b8ced0 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -516,9 +516,9 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubProperty &subProperty) { - IMSA_HILOGI("run in %{public}s", __func__); + IMSA_HILOGD("run in"); std::string type = "imeChange"; - auto work = GetUVwork(type, [&property, &subProperty](UvEntry &entry) { + uv_work_t *work = GetUVwork(type, [&property, &subProperty](UvEntry &entry) { entry.property = property; entry.subProperty = subProperty; }); @@ -529,16 +529,16 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro uv_queue_work( loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [&work](UvEntry *data) { - SAFE_DELETE(data); - SAFE_DELETE(work); + std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { + delete data; + delete work; }); if (entry == nullptr) { IMSA_HILOGE("OnInputStart:: entryptr is null"); return; } auto getImeChangeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc < 2) { return false; } @@ -558,7 +558,7 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro uv_work_t *JsGetInputMethodSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) { - IMSA_HILOGD("run in %{public}s: %{public}s", __func__, type.c_str()); + IMSA_HILOGD("run in, type: %{public}s", type.c_str()); UvEntry *entry = nullptr; { std::lock_guard lock(mutex_); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_utils.h b/interfaces/kits/js/napi/inputmethodclient/js_utils.h index f3e811cc3..183fea32f 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_utils.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_utils.h @@ -81,14 +81,6 @@ private: static constexpr uint8_t MAX_ARGMENT_COUNT = 10; }; - -#define SAFE_DELETE(p) \ - do { \ - if ((p) != nullptr) { \ - delete (p); \ - (p) = nullptr; \ - } \ - } while (0) } // namespace MiscServices } // namespace OHOS #endif // INTERFACE_KITS_JS_UTILS_H \ No newline at end of file -- Gitee