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;
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?