From f6d45f799022ac58da533dfe30c8b34c10055f67 Mon Sep 17 00:00:00 2001 From: Tony Butler Date: Sun, 24 Oct 2021 23:22:58 -0600 Subject: [PATCH] Fix various memory leaks --- src/base/net/dns/DnsUvBackend.cpp | 9 ++++++++- src/base/net/dns/DnsUvBackend.h | 1 + src/crypto/cn/CnHash.cpp | 8 ++++++++ src/crypto/cn/CnHash.h | 1 + src/crypto/randomx/randomx.cpp | 1 + 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/base/net/dns/DnsUvBackend.cpp b/src/base/net/dns/DnsUvBackend.cpp index cb1c0c3eb..8de95df54 100644 --- a/src/base/net/dns/DnsUvBackend.cpp +++ b/src/base/net/dns/DnsUvBackend.cpp @@ -28,13 +28,19 @@ namespace xmrig { +static Storage* storage = nullptr; Storage& DnsUvBackend::getStorage() { - static Storage* storage = new Storage(); + if (storage == nullptr) storage = new Storage(); return *storage; } +void DnsUvBackend::releaseStorage() +{ + delete storage; +} + static addrinfo hints{}; @@ -56,6 +62,7 @@ xmrig::DnsUvBackend::DnsUvBackend() xmrig::DnsUvBackend::~DnsUvBackend() { getStorage().release(m_key); + releaseStorage(); } diff --git a/src/base/net/dns/DnsUvBackend.h b/src/base/net/dns/DnsUvBackend.h index f3733f5a4..3c2436c7e 100644 --- a/src/base/net/dns/DnsUvBackend.h +++ b/src/base/net/dns/DnsUvBackend.h @@ -62,6 +62,7 @@ private: uintptr_t m_key; static Storage& getStorage(); + void releaseStorage(); }; diff --git a/src/crypto/cn/CnHash.cpp b/src/crypto/cn/CnHash.cpp index 0961f6f6c..ab2722251 100644 --- a/src/crypto/cn/CnHash.cpp +++ b/src/crypto/cn/CnHash.cpp @@ -316,6 +316,14 @@ xmrig::CnHash::CnHash() } +xmrig::CnHash::~CnHash() +{ + for (auto const& x : m_map) { + delete m_map[x.first]; + } +} + + xmrig::cn_hash_fun xmrig::CnHash::fn(const Algorithm &algorithm, AlgoVariant av, Assembly::Id assembly) { assert(cnHash.m_map.count(algorithm)); diff --git a/src/crypto/cn/CnHash.h b/src/crypto/cn/CnHash.h index 5aac95443..1f00a077b 100644 --- a/src/crypto/cn/CnHash.h +++ b/src/crypto/cn/CnHash.h @@ -59,6 +59,7 @@ public: }; CnHash(); + virtual ~CnHash(); static cn_hash_fun fn(const Algorithm &algorithm, AlgoVariant av, Assembly::Id assembly); diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp index bc1dde87e..b7a3ecaf6 100644 --- a/src/crypto/randomx/randomx.cpp +++ b/src/crypto/randomx/randomx.cpp @@ -410,6 +410,7 @@ extern "C" { } void randomx_release_cache(randomx_cache* cache) { + delete cache->jit; delete cache; }