From 8af9e9b27d97a13d07b796b6a51c51ee1f513735 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 7 Jun 2022 19:40:13 +0200 Subject: [PATCH] Fixed memory leaks --- src/json_rpc_request.cpp | 4 ++++ src/json_rpc_request.h | 2 +- src/p2p_server.cpp | 6 +++++- src/tcp_server.inl | 2 +- src/uv_util.h | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/json_rpc_request.cpp b/src/json_rpc_request.cpp index 9e8d7a3..3675731 100644 --- a/src/json_rpc_request.cpp +++ b/src/json_rpc_request.cpp @@ -338,6 +338,10 @@ void CurlContext::on_close(uv_handle_t* h) void Call(const std::string& address, int port, const std::string& req, const std::string& auth, CallbackBase* cb, CallbackBase* close_cb, uv_loop_t* loop) { + if (!loop) { + loop = uv_default_loop(); + } + CallOnLoop(loop, [=]() { diff --git a/src/json_rpc_request.h b/src/json_rpc_request.h index 3c4fd55..70f6b90 100644 --- a/src/json_rpc_request.h +++ b/src/json_rpc_request.h @@ -40,7 +40,7 @@ private: void Call(const std::string& address, int port, const std::string& req, const std::string& auth, CallbackBase* cb, CallbackBase* close_cb, uv_loop_t* loop); template -FORCEINLINE void call(const std::string& address, int port, const std::string& req, const std::string& auth, T&& cb, U&& close_cb, uv_loop_t* loop = uv_default_loop_checked()) +FORCEINLINE void call(const std::string& address, int port, const std::string& req, const std::string& auth, T&& cb, U&& close_cb, uv_loop_t* loop = nullptr) { Call(address, port, req, auth, new Callback(std::move(cb)), new Callback(std::move(close_cb)), loop); } diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index a06c20f..4ac17bb 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -124,6 +124,10 @@ P2PServer::~P2PServer() delete m_block; delete m_cache; + + for (const Broadcast* data : m_broadcastQueue) { + delete data; + } } void P2PServer::add_cached_block(const PoolBlock& block) @@ -785,7 +789,7 @@ void P2PServer::on_broadcast() ON_SCOPE_LEAVE([&broadcast_queue]() { - for (Broadcast* data : broadcast_queue) { + for (const Broadcast* data : broadcast_queue) { delete data; } }); diff --git a/src/tcp_server.inl b/src/tcp_server.inl index 738e9f9..79a6c10 100644 --- a/src/tcp_server.inl +++ b/src/tcp_server.inl @@ -473,7 +473,7 @@ void TCPServer::shutdown_tcp() } else { LOGWARN(1, "timed out while waiting for event loop to stop"); - uv_async_init(&m_loop, &asy, nullptr); + uv_async_init(&m_loop, &asy, [](uv_async_t* h) { uv_close(reinterpret_cast(h), nullptr); }); uv_stop(&m_loop); uv_async_send(&asy); break; diff --git a/src/uv_util.h b/src/uv_util.h index 0d0e780..2d509be 100644 --- a/src/uv_util.h +++ b/src/uv_util.h @@ -139,7 +139,7 @@ UV_LoopUserData* GetLoopUserData(uv_loop_t* loop, bool create = true); template void CallOnLoop(uv_loop_t* loop, T&& callback) { - UV_LoopUserData* data = GetLoopUserData(loop); + UV_LoopUserData* data = GetLoopUserData(loop, false); UV_LoopCallbackBase* cb = new UV_LoopCallback(std::move(callback)); {