From 34732a1ee7f97b627b56769d54af7e48263318f6 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 11 Jun 2023 10:47:17 +0200 Subject: [PATCH] fix: Corrected some memory leaks --- lib/external/pattern_language | 2 +- lib/libimhex/include/hex/api/content_registry.hpp | 7 ++++--- lib/libimhex/include/hex/api/event.hpp | 6 +++--- lib/libimhex/source/api/content_registry.cpp | 8 ++++---- plugins/builtin/source/content/views/view_hashes.cpp | 8 ++++---- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 70fc07d46..f5f8787ce 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 70fc07d46f48901d79230ec9f45abd203dbe8f67 +Subproject commit f5f8787ce66362c9c79e0bb683ab06b63eefa984 diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 4d6b1bc3c..0c019c3b0 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -749,6 +749,7 @@ namespace hex { class Hash { public: explicit Hash(std::string unlocalizedName) : m_unlocalizedName(std::move(unlocalizedName)) {} + virtual ~Hash() = default; class Function { public: @@ -804,9 +805,9 @@ namespace hex { namespace impl { - std::vector &getHashes(); + std::vector> &getHashes(); - void add(Hash* hash); + void add(std::unique_ptr &&hash); } @@ -817,7 +818,7 @@ namespace hex { */ template void add(Args && ... args) { - impl::add(new T(std::forward(args)...)); + impl::add(std::make_unique(std::forward(args)...)); } } diff --git a/lib/libimhex/include/hex/api/event.hpp b/lib/libimhex/include/hex/api/event.hpp index 048113421..4a0e02ba3 100644 --- a/lib/libimhex/include/hex/api/event.hpp +++ b/lib/libimhex/include/hex/api/event.hpp @@ -72,7 +72,7 @@ namespace hex { */ class EventManager { public: - using EventList = std::list>; + using EventList = std::list>>; /** * @brief Subscribes to an event @@ -82,7 +82,7 @@ namespace hex { */ template static EventList::iterator subscribe(typename E::Callback function) { - return s_events.insert(s_events.end(), std::make_pair(E::Id, new E(function))); + return s_events.insert(s_events.end(), std::make_pair(E::Id, std::make_unique(function))); } /** @@ -131,7 +131,7 @@ namespace hex { static void post(auto &&...args) noexcept { for (const auto &[id, event] : s_events) { if (id == E::Id) { - (*static_cast(event))(std::forward(args)...); + (*static_cast(event.get()))(std::forward(args)...); } } diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 21b99e2aa..2e73c5cc3 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -774,14 +774,14 @@ namespace hex { namespace impl { - std::vector &getHashes() { - static std::vector hashes; + std::vector> &getHashes() { + static std::vector> hashes; return hashes; } - void add(Hash *hash) { - getHashes().push_back(hash); + void add(std::unique_ptr &&hash) { + getHashes().emplace_back(std::move(hash)); } } diff --git a/plugins/builtin/source/content/views/view_hashes.cpp b/plugins/builtin/source/content/views/view_hashes.cpp index 0812370da..0e6c5be68 100644 --- a/plugins/builtin/source/content/views/view_hashes.cpp +++ b/plugins/builtin/source/content/views/view_hashes.cpp @@ -91,15 +91,15 @@ namespace hex::plugin::builtin { const auto &hashes = ContentRegistry::Hashes::impl::getHashes(); if (this->m_selectedHash == nullptr && !hashes.empty()) { - this->m_selectedHash = hashes.front(); + this->m_selectedHash = hashes.front().get(); } if (ImGui::Begin(View::toWindowName("hex.builtin.view.hashes.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { if (ImGui::BeginCombo("hex.builtin.view.hashes.function"_lang, this->m_selectedHash != nullptr ? LangEntry(this->m_selectedHash->getUnlocalizedName()) : "")) { - for (const auto hash : hashes) { - if (ImGui::Selectable(LangEntry(hash->getUnlocalizedName()), this->m_selectedHash == hash)) { - this->m_selectedHash = hash; + for (const auto &hash : hashes) { + if (ImGui::Selectable(LangEntry(hash->getUnlocalizedName()), this->m_selectedHash == hash.get())) { + this->m_selectedHash = hash.get(); this->m_newHashName.clear(); } }