memory fault when passing var to a coroutine

Piotr Gosławski via Boost-users <[email protected]>
Newsgroups gmane.comp.lib.boost.user
Message-ID <CAPHUQ2H6ME+ohEXWQxmY3vWEuYr73a9ta4VNUQJOKvHAXXr+gw@mail.gmail.com>
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 mailing list
[email protected]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.