chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.99k stars 1.23k forks source link

Memory leak in asio::awaitable move assignment operator when lhs is valid #1423

Open erinacio opened 10 months ago

erinacio commented 10 months ago

Example:

// Clang 16, C++20, -fsanitize=address
#include <asio.hpp>

asio::awaitable<void> async_foo() {
  co_return;
}

asio::awaitable<void> async_main() {
  asio::awaitable<void> a{async_foo()};  // allocates here
  a = async_foo();  // leaks here
  co_await std::move(a);
}

int main() {
  asio::io_context io{1};
  asio::co_spawn(io, async_main(), asio::detached);
  io.run();
  return 0;
}

I didn't found any official document indicating such behavior, so I believe that is unintentional and needs a fix.