Commit 5da4203c authored by Joseph Noir's avatar Joseph Noir

Fix memory leak in the asio based multiplexer

parent b23f2def
...@@ -116,13 +116,13 @@ class multiplexer { ...@@ -116,13 +116,13 @@ class multiplexer {
/** /**
* Simple wrapper for runnables * Simple wrapper for runnables
*/ */
struct runnable : memory_managed { struct runnable : ref_counted {
static constexpr auto memory_cache_flag = detail::needs_embedding; static constexpr auto memory_cache_flag = detail::needs_embedding;
virtual void run() = 0; virtual void run() = 0;
virtual ~runnable(); virtual ~runnable();
}; };
using runnable_ptr = std::unique_ptr<runnable, detail::disposer>; using runnable_ptr = intrusive_ptr<runnable>;
/** /**
* Makes sure the multipler does not exit its event loop until * Makes sure the multipler does not exit its event loop until
...@@ -176,7 +176,8 @@ class multiplexer { ...@@ -176,7 +176,8 @@ class multiplexer {
f(); f();
} }
}; };
dispatch_runnable(runnable_ptr{detail::memory::create<impl>(std::move(fun))}); dispatch_runnable(runnable_ptr{detail::memory::create<impl>(std::move(fun)),
false});
} }
/** /**
......
...@@ -266,10 +266,8 @@ asio_multiplexer::add_tcp_doorman(broker* self, uint16_t port, const char* in, ...@@ -266,10 +266,8 @@ asio_multiplexer::add_tcp_doorman(broker* self, uint16_t port, const char* in,
} }
void asio_multiplexer::dispatch_runnable(runnable_ptr ptr) { void asio_multiplexer::dispatch_runnable(runnable_ptr ptr) {
auto r = ptr.release();
backend().post([=]() { backend().post([=]() {
r->run(); ptr->run();
r->request_deletion(false);
}); });
} }
......
...@@ -592,7 +592,7 @@ void default_multiplexer::wr_dispatch_request(runnable* ptr) { ...@@ -592,7 +592,7 @@ void default_multiplexer::wr_dispatch_request(runnable* ptr) {
# endif # endif
if (res <= 0) { if (res <= 0) {
// pipe closed, discard runnable // pipe closed, discard runnable
ptr->request_deletion(false); ptr->deref();
} else if (static_cast<size_t>(res) < sizeof(ptrval)) { } else if (static_cast<size_t>(res) < sizeof(ptrval)) {
// must not happen: wrote invalid pointer to pipe // must not happen: wrote invalid pointer to pipe
std::cerr << "[CAF] Fatal error: wrote invalid data to pipe" << std::endl; std::cerr << "[CAF] Fatal error: wrote invalid data to pipe" << std::endl;
...@@ -666,7 +666,7 @@ void default_multiplexer::handle_socket_event(native_socket fd, int mask, ...@@ -666,7 +666,7 @@ void default_multiplexer::handle_socket_event(native_socket fd, int mask,
CAF_LOG_DEBUG("read message from pipe"); CAF_LOG_DEBUG("read message from pipe");
auto cb = rd_dispatch_request(); auto cb = rd_dispatch_request();
cb->run(); cb->run();
cb->request_deletion(false); cb->deref();
} }
} }
if (mask & output_mask) { if (mask & output_mask) {
...@@ -713,7 +713,7 @@ default_multiplexer::~default_multiplexer() { ...@@ -713,7 +713,7 @@ default_multiplexer::~default_multiplexer() {
nonblocking(m_pipe.first, true); nonblocking(m_pipe.first, true);
auto ptr = rd_dispatch_request(); auto ptr = rd_dispatch_request();
while (ptr) { while (ptr) {
ptr->request_deletion(false); ptr->deref();
ptr = rd_dispatch_request(); ptr = rd_dispatch_request();
} }
closesocket(m_pipe.first); closesocket(m_pipe.first);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment