asio
asio copied to clipboard
AddressSanitizer error when using co_spawn with capturing lambda
I get AddressSanitizer: attempting free on address which was not malloc
with the following code:
Error is reproducible in Godbolt
It doesn't happen if we remove the captures.
#include <boost/asio.hpp>
#include <boost/asio/experimental/parallel_group.hpp>
namespace asio = boost::asio;
namespace ex = asio::experimental;
asio::awaitable< void >
async_main()
{
auto executor = co_await asio::this_coro::executor;
co_await asio::co_spawn(
executor,
[s = std::string{}]() -> asio::awaitable< void >
{
co_return;
},
asio::use_awaitable);
}
int
main()
{
auto io_context = asio::io_context{};
asio::co_spawn(io_context, async_main(), asio::detached);
io_context.run();
}
I think I stumbled upon this nasty error as well. Any opinions @chriskohlhoff? Thanks!
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rcoro-capture
The function passed to co_spawn is just an initiation function so this is working as intended.