From e4cbd36d22600e53bbd6232751887e3831836f1b Mon Sep 17 00:00:00 2001 From: MoneroOcean Date: Thu, 11 Oct 2018 18:57:09 +0200 Subject: [PATCH] Avoid deleting hashrate object --- src/workers/Hashrate.cpp | 40 +++++++++++++++++++++++----------------- src/workers/Hashrate.h | 2 +- src/workers/Workers.cpp | 12 +----------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 10fd8279..2f426204 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -48,18 +48,10 @@ inline static const char *format(double h, char *buf, size_t size) Hashrate::Hashrate(size_t threads, xmrig::Controller *controller) : m_highest(0.0), - m_threads(threads), + m_threads(0), m_controller(controller) { - m_counts = new uint64_t*[threads]; - m_timestamps = new uint64_t*[threads]; - m_top = new uint32_t[threads]; - - for (size_t i = 0; i < threads; i++) { - m_counts[i] = new uint64_t[kBucketSize](); - m_timestamps[i] = new uint64_t[kBucketSize](); - m_top[i] = 0; - } + set_threads(threads); const int printTime = controller->config()->printTime(); @@ -71,15 +63,29 @@ Hashrate::Hashrate(size_t threads, xmrig::Controller *controller) : } } -Hashrate::~Hashrate() +void Hashrate::set_threads(const size_t threads) { - for (size_t i = 0; i < m_threads; i++) { - delete [] m_counts[i]; - delete [] m_timestamps[i]; + if (m_threads) { + for (size_t i = 0; i < m_threads; i++) { + delete [] m_counts[i]; + delete [] m_timestamps[i]; + } + delete [] m_counts; + delete [] m_timestamps; + delete [] m_top; + } + + m_threads = threads; + + m_counts = new uint64_t*[threads]; + m_timestamps = new uint64_t*[threads]; + m_top = new uint32_t[threads]; + + for (size_t i = 0; i < threads; i++) { + m_counts[i] = new uint64_t[kBucketSize](); + m_timestamps[i] = new uint64_t[kBucketSize](); + m_top[i] = 0; } - delete [] m_counts; - delete [] m_timestamps; - delete [] m_top; } diff --git a/src/workers/Hashrate.h b/src/workers/Hashrate.h index 8b323a80..fa8c9053 100644 --- a/src/workers/Hashrate.h +++ b/src/workers/Hashrate.h @@ -44,7 +44,7 @@ public: }; Hashrate(size_t threads, xmrig::Controller *controller); - ~Hashrate(); + void set_threads(size_t threads); double calc(size_t ms) const; double calc(size_t threadId, size_t ms) const; void add(size_t threadId, uint64_t count, uint64_t timestamp); diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index fffe3094..2bd3ed15 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -210,14 +210,6 @@ void Workers::start(xmrig::Controller *controller) void Workers::soft_stop() // stop current workers leaving uv stuff intact (used in switch_algo) { - uv_timer_stop(&m_timer); - - if (m_hashrate) { - m_hashrate->stop(); - delete m_hashrate; - m_hashrate = nullptr; - } - m_sequence = 0; m_paused = 0; @@ -262,7 +254,7 @@ void Workers::switch_algo(const xmrig::Algorithm& algorithm) m_status.ways += thread->multiway(); } - m_hashrate = new Hashrate(threads.size(), m_controller); + m_hashrate.set_threads(threads.size()); uint32_t offset = 0; @@ -273,8 +265,6 @@ void Workers::switch_algo(const xmrig::Algorithm& algorithm) m_workers.push_back(handle); handle->start(Workers::onReady); } - - uv_timer_start(&m_timer, Workers::onTick, 500, 500); }