diff --git a/frameworks/core/interfaces/native/node/custom_dialog_model.cpp b/frameworks/core/interfaces/native/node/custom_dialog_model.cpp index b55e345d36842701fffbfff94bf7b8e2c205e285..c4ded094780358e81f4bb3335bc7b187bb65b764 100644 --- a/frameworks/core/interfaces/native/node/custom_dialog_model.cpp +++ b/frameworks/core/interfaces/native/node/custom_dialog_model.cpp @@ -12,6 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#include +#include +#include #include "core/interfaces/native/node/custom_dialog_model.h" #include "interfaces/native/node/dialog_model.h" @@ -60,9 +64,40 @@ namespace { DialogProperties g_dialogProperties; } // namespace +class NativeDialogManager { +public: + static void RegisterDialog(ArkUIDialogHandle handle) { + std::lock_guard lock(nativeDialogMutex_); + nativeDialogMap_[handle] = std::make_shared>(true); + } + + static void UnregisterDialog(ArkUIDialogHandle handle) { + std::lock_guard lock(nativeDialogMutex_); + if (auto it = nativeDialogMap_.find(handle); it != nativeDialogMap_.end()) { + it->second->store(false); + nativeDialogMap_.erase(it); + } + delete handle; + } + + static std::shared_ptr> GetSafeController(ArkUIDialogHandle handle) { + std::lock_guard lock(nativeDialogMutex_); + if (auto it = nativeDialogMap_.find(handle); it != nativeDialogMap_.end()) { + return it->second; + } + return nullptr; + } +private: + static std::mutex nativeDialogMutex_; + static std::unordered_map>> nativeDialogMap_; +}; + +std::mutex NativeDialogManager::nativeDialogMutex_; +std::unordered_map>> NativeDialogManager::nativeDialogMap_; + ArkUIDialogHandle CreateDialog() { - return new _ArkUIDialog({ .dialogHandle = nullptr, + auto* dialog = new _ArkUIDialog({ .dialogHandle = nullptr, .contentHandle = nullptr, .alignment = DEFAULT_DIALOG_ALIGNMENT, .offsetX = 0.0f, @@ -120,6 +155,8 @@ ArkUIDialogHandle CreateDialog() .focusable = true, .dialogState = nullptr, }); + NativeDialogManager::RegisterDialog(dialog); + return dialog; } void DisposeDialog(ArkUIDialogHandle controllerHandler) @@ -150,7 +187,7 @@ void DisposeDialog(ArkUIDialogHandle controllerHandler) controllerHandler->onWillDismissCall = nullptr; controllerHandler->onWillDismissCallByNDK = nullptr; controllerHandler->userData = nullptr; - delete controllerHandler; + NativeDialogManager::UnregisterDialog(controllerHandler); } DialogAlignment GetDialogAlignment(int32_t alignment) @@ -201,10 +238,13 @@ void ParseDialogOnWillDismiss(DialogProperties& dialogProperties, ArkUIDialogHan if (!controllerHandler->onWillDismissCall) { return; } - dialogProperties.onWillDismiss = [controllerHandler](int32_t reason, int32_t instanceId) { + auto safeController = NativeDialogManager::GetSafeController(controllerHandler); + CHECK_NULL_VOID(safeController); + dialogProperties.onWillDismiss = [safeController, controllerHandler](int32_t reason, int32_t instanceId) { CHECK_NULL_VOID(controllerHandler); - CHECK_NULL_VOID(controllerHandler->onWillDismissCall); - (*(controllerHandler->onWillDismissCall))(reason); + if (safeController->load() && controllerHandler->onWillDismissCall){ + (*(controllerHandler->onWillDismissCall))(reason); + } }; } diff --git a/test/unittest/interfaces/dialog_model_test.cpp b/test/unittest/interfaces/dialog_model_test.cpp index 35d5e4ff29236cf53573d45d4b7c464c6313b206..8fd11a8a7099b6936eab249f83e5e487fc9649f6 100644 --- a/test/unittest/interfaces/dialog_model_test.cpp +++ b/test/unittest/interfaces/dialog_model_test.cpp @@ -726,7 +726,7 @@ HWTEST_F(DialogModelTest, DialogModelTest047, TestSize.Level1) ArkUI_LengthMetricUnit unit = ARKUI_LENGTH_METRIC_UNIT_DEFAULT; float distance = 1.0f; int32_t ret = SetKeyboardAvoidDistance(nativeDialogHandle, distance, unit); - ASSERT_NE(ret, 1); + ASSERT_NE(ret, 1); // success Dispose(nativeDialogHandle); nativeDialogHandle = nullptr; }