From 9a9c1c092ca4f5fe8b04b1354c7253fd6e62a22b Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 29 Dec 2024 23:52:22 +0100 Subject: [PATCH] Fix move constructor/operator to properly release the combo handle, use std::forward_list instead of vector for the list --- source/plugin/ButtonComboManager.cpp | 26 ++++++++++++++++++-------- source/plugin/ButtonComboManager.h | 4 ++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/source/plugin/ButtonComboManager.cpp b/source/plugin/ButtonComboManager.cpp index 9590776..cff1b31 100644 --- a/source/plugin/ButtonComboManager.cpp +++ b/source/plugin/ButtonComboManager.cpp @@ -328,32 +328,42 @@ public: } ~ButtonComboWrapper() { + ReleaseButtonComboHandle(); + } + + void ReleaseButtonComboHandle() { if (mButtonComboHandle != nullptr) { if (const auto res = ButtonComboModule_RemoveButtonCombo(mButtonComboHandle); res != BUTTON_COMBO_MODULE_ERROR_SUCCESS) { DEBUG_FUNCTION_LINE_WARN("ButtonComboModule_RemoveButtonCombo for %08X returned: %s", mButtonComboHandle, ButtonComboModule_GetStatusStr(res)); } + mButtonComboHandle = ButtonComboModule_ComboHandle(nullptr); } } ButtonComboWrapper(const ButtonComboWrapper &) = delete; + ButtonComboWrapper &operator=(ButtonComboWrapper &src) = delete; ButtonComboWrapper(ButtonComboWrapper &&src) noexcept : mCreationError(src.mCreationError), - mButtonComboHandle(src.mButtonComboHandle), mContextData(std::move(src.mContextData)), mHandle(std::move(src.mHandle)) { - this->mCreationError = BUTTON_COMBO_MODULE_ERROR_UNKNOWN_ERROR; - this->mButtonComboHandle = ButtonComboModule_ComboHandle(nullptr); + ReleaseButtonComboHandle(); + + mButtonComboHandle = ButtonComboModule_ComboHandle(src.mButtonComboHandle.handle); + + src.mCreationError = BUTTON_COMBO_MODULE_ERROR_UNKNOWN_ERROR; + src.mButtonComboHandle = ButtonComboModule_ComboHandle(nullptr); } ButtonComboWrapper &operator=(ButtonComboWrapper &&src) noexcept { if (this != &src) { + ReleaseButtonComboHandle(); this->mCreationError = src.mCreationError; - this->mButtonComboHandle = src.mButtonComboHandle; + this->mButtonComboHandle = ButtonComboModule_ComboHandle(src.mButtonComboHandle.handle); this->mContextData = std::move(src.mContextData); this->mHandle = std::move(src.mHandle); - this->mCreationError = BUTTON_COMBO_MODULE_ERROR_UNKNOWN_ERROR; - this->mButtonComboHandle = ButtonComboModule_ComboHandle(nullptr); + src.mCreationError = BUTTON_COMBO_MODULE_ERROR_UNKNOWN_ERROR; + src.mButtonComboHandle = ButtonComboModule_ComboHandle(nullptr); } return *this; } @@ -453,8 +463,8 @@ ButtonComboManager &ButtonComboManager::operator=(ButtonComboManager &&src) { WUPSButtonCombo_Error ButtonComboManager::AddButtonComboHandle(const WUPSButtonCombo_ComboOptions &options, WUPSButtonCombo_ComboHandle &outHandle, WUPSButtonCombo_ComboStatus &outStatus) { - mComboWrappers.emplace_back(options, outStatus); - const auto &addedItem = mComboWrappers.back(); + mComboWrappers.emplace_front(options, outStatus); + const auto &addedItem = mComboWrappers.front(); const auto handle = addedItem.getHandle(); if (const auto res = addedItem.GetCreationError(); res != WUPS_BUTTON_COMBO_ERROR_SUCCESS) { if (!remove_first_if(mComboWrappers, [&handle](const auto &comboWrapper) { return comboWrapper.getHandle() == handle; })) { diff --git a/source/plugin/ButtonComboManager.h b/source/plugin/ButtonComboManager.h index 8bd50ff..c3aa319 100644 --- a/source/plugin/ButtonComboManager.h +++ b/source/plugin/ButtonComboManager.h @@ -6,9 +6,9 @@ #include #include -#include #include +#include class ButtonComboWrapper; @@ -71,6 +71,6 @@ public: [[nodiscard]] uint32_t getHandle() const; private: - std::vector mComboWrappers; + std::forward_list mComboWrappers; std::unique_ptr mHandle = std::make_unique(); };