Boost logo

Boost Users :

From: Piotr Gosławski (goslawski.p_at_[hidden])
Date: 2022-08-23 07:43:16


Hello,

Using GCC 10.1 and Boost 1.77 I get a mem fault when executing the
following code:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/awaitable.hpp>

struct TestStruct {
std::string strField;
};

boost::asio::awaitable<bool> TestFunction(TestStruct param) {
std::cout << param.strField << std::endl;
co_return true;
}

boost::asio::awaitable<void> TestCoroutine() {
// This causes mem issue
co_await TestFunction(TestStruct{"some string"});
}

int main()
{
boost::asio::io_context io_context;

boost::asio::co_spawn(
io_context,
[]() { return TestCoroutine(); },
boost::asio::detached);

io_context.run();
return 0;
} Also tested with GCC 12.1 and Boost 1.79 - the result is the same (
https://godbolt.org/z/69MKbz7qE).

Works if I use a temp variable:
boost::asio::awaitable<void> TestCoroutine() {
auto testStruct = TestStruct{"some string"};
co_await TestFunction(testStruct);
}
Or if I add a constructor to test struct:

struct TestStruct {
    explicit TestStruct(const std::string& str) : strField(str) {}
    std::string strField;
};

boost::asio::awaitable<void> TestCoroutine() {
    co_await TestFunction(TestStruct("some string"));
}

Is there any coroutines rule that I am violating here? If yes - is there
any compiler option I can use to get a warning / error message?

Full code with cmake can be found here:
https://github.com/piotrgoslawski/mem_issue

Thank you



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net