From de15a7c280975b70ea753824b53cd4216b976e21 Mon Sep 17 00:00:00 2001
From: Yimura <24669514+Yimura@users.noreply.github.com>
Date: Fri, 21 Oct 2022 14:09:20 +0200
Subject: [PATCH] fix(BytePatch): To ensure proper cleanup make use of
unique_ptr (#491)
---
BigBaseV2/src/memory/byte_patch.hpp | 22 +++++++---------------
1 file changed, 7 insertions(+), 15 deletions(-)
diff --git a/BigBaseV2/src/memory/byte_patch.hpp b/BigBaseV2/src/memory/byte_patch.hpp
index 717dfb6b..f91ed9d2 100644
--- a/BigBaseV2/src/memory/byte_patch.hpp
+++ b/BigBaseV2/src/memory/byte_patch.hpp
@@ -10,32 +10,24 @@ namespace memory
memcpy(m_address, m_original_bytes.data(), m_original_bytes.size());
}
- ///
- /// To guarantee proper restoration of bytes all shared_ptr instances will be invalidated that point to this object.
- ///
void restore() const
{
if (const auto it = std::find(m_patches.begin(), m_patches.end(), this); it != m_patches.end())
{
- it->reset();
m_patches.erase(it);
}
}
template
- static std::shared_ptr make(TAddr address, std::remove_pointer_t> value)
+ static const std::unique_ptr& make(TAddr address, std::remove_pointer_t> value)
{
- auto patch = std::shared_ptr(new byte_patch(address, value));
- m_patches.emplace_back(patch);
- return patch;
+ return m_patches.emplace_back(
+ std::unique_ptr(new byte_patch(address, value)));
}
static void restore_all()
{
- for (const auto& patch : m_patches)
- {
- patch->restore();
- }
+ m_patches.clear();
}
private:
@@ -51,16 +43,16 @@ namespace memory
}
protected:
- static inline std::vector> m_patches;
+ static inline std::vector> m_patches;
private:
void* m_address;
std::vector m_original_bytes;
- friend bool operator== (const std::shared_ptr a, const byte_patch* b);
+ friend bool operator== (const std::unique_ptr& a, const byte_patch* b);
};
- bool operator== (const std::shared_ptr a, const byte_patch* b)
+ bool operator== (const std::unique_ptr& a, const byte_patch* b)
{
return a->m_address == b->m_address;
}