asio icon indicating copy to clipboard operation
asio copied to clipboard

AddressSanitizer error when using co_spawn with capturing lambda

Open nejati-deriv opened this issue 2 years ago • 2 comments

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();
}

nejati-deriv avatar Oct 06 '22 09:10 nejati-deriv

I think I stumbled upon this nasty error as well. Any opinions @chriskohlhoff? Thanks!

rbeeli avatar May 03 '24 02:05 rbeeli

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.

andrei-datcu avatar May 03 '24 05:05 andrei-datcu