From 667f636c626cd231a3727d624d70c89619723f9e Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 9 Feb 2023 21:45:50 +0700 Subject: [PATCH] Fixed DnsUvBackend storage cleanup. --- src/base/net/dns/DnsUvBackend.cpp | 29 ++++++++++++++++++----------- src/base/net/tools/Storage.h | 9 ++++++--- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/base/net/dns/DnsUvBackend.cpp b/src/base/net/dns/DnsUvBackend.cpp index 8de95df54..33f27f95e 100644 --- a/src/base/net/dns/DnsUvBackend.cpp +++ b/src/base/net/dns/DnsUvBackend.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,18 +28,19 @@ namespace xmrig { -static Storage* storage = nullptr; -Storage& DnsUvBackend::getStorage() +static Storage *storage = nullptr; + + +Storage &DnsUvBackend::getStorage() { - if (storage == nullptr) storage = new Storage(); + if (storage == nullptr) { + storage = new Storage(); + } + return *storage; } -void DnsUvBackend::releaseStorage() -{ - delete storage; -} static addrinfo hints{}; @@ -61,8 +62,14 @@ xmrig::DnsUvBackend::DnsUvBackend() xmrig::DnsUvBackend::~DnsUvBackend() { - getStorage().release(m_key); - releaseStorage(); + assert(storage); + + storage->release(m_key); + + if (storage->isEmpty()) { + delete storage; + storage = nullptr; + } } diff --git a/src/base/net/tools/Storage.h b/src/base/net/tools/Storage.h index e23e86665..390101c30 100644 --- a/src/base/net/tools/Storage.h +++ b/src/base/net/tools/Storage.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright 2018-2023 SChernykh + * Copyright 2016-2023 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -42,7 +42,7 @@ public: } - inline void *ptr(uintptr_t id) { return reinterpret_cast(id); } + inline TYPE *ptr(uintptr_t id) { return reinterpret_cast(id); } inline TYPE *get(const void *id) const { return get(reinterpret_cast(id)); } @@ -56,6 +56,9 @@ public: return m_data.at(id); } + inline bool isEmpty() const { return m_data.empty(); } + inline size_t size() const { return m_data.size(); } + inline void remove(const void *id) { delete release(reinterpret_cast(id)); } inline void remove(uintptr_t id) { delete release(id); }