Skip to content

Commit

Permalink
Fix move constructor/operator to properly release the combo handle, u…
Browse files Browse the repository at this point in the history
…se std::forward_list instead of vector for the list
  • Loading branch information
Maschell committed Dec 29, 2024
1 parent 7ed6058 commit e4023be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 18 additions & 8 deletions source/plugin/ButtonComboManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,32 +328,42 @@ class ButtonComboWrapper {
}

~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;
}
Expand Down Expand Up @@ -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; })) {
Expand Down
4 changes: 2 additions & 2 deletions source/plugin/ButtonComboManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include <functional>
#include <memory>
#include <vector>

#include <cstdint>
#include <forward_list>


class ButtonComboWrapper;
Expand Down Expand Up @@ -71,6 +71,6 @@ class ButtonComboManager {
[[nodiscard]] uint32_t getHandle() const;

private:
std::vector<ButtonComboWrapper> mComboWrappers;
std::forward_list<ButtonComboWrapper> mComboWrappers;
std::unique_ptr<uint32_t> mHandle = std::make_unique<uint32_t>();
};

0 comments on commit e4023be

Please sign in to comment.