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 {
/**
* Simple wrapper for runnables
*/
struct runnable : memory_managed {
struct runnable : ref_counted {
static constexpr auto memory_cache_flag = detail::needs_embedding;
virtual void run() = 0;
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
......@@ -176,7 +176,8 @@ class multiplexer {
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,
}
void asio_multiplexer::dispatch_runnable(runnable_ptr ptr) {
auto r = ptr.release();
backend().post([=]() {
r->run();
r->request_deletion(false);
ptr->run();
});
}
......
......@@ -592,7 +592,7 @@ void default_multiplexer::wr_dispatch_request(runnable* ptr) {
# endif
if (res <= 0) {
// pipe closed, discard runnable
ptr->request_deletion(false);
ptr->deref();
} else if (static_cast<size_t>(res) < sizeof(ptrval)) {
// must not happen: wrote invalid pointer to pipe
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,
CAF_LOG_DEBUG("read message from pipe");
auto cb = rd_dispatch_request();
cb->run();
cb->request_deletion(false);
cb->deref();
}
}
if (mask & output_mask) {
......@@ -713,7 +713,7 @@ default_multiplexer::~default_multiplexer() {
nonblocking(m_pipe.first, true);
auto ptr = rd_dispatch_request();
while (ptr) {
ptr->request_deletion(false);
ptr->deref();
ptr = rd_dispatch_request();
}
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