C++20 coroutines crashing with std::shared_ptr when returning from lambda
I'm testing a new approach and I've hit a wall trying to I've been struggling with this for a few days now and could really use some help... I'm encountering a crash when using C++20 coroutines with `std::shared_ptr`. Specifically, I'm trying to return a coroutine from a lambda that captures a `std::shared_ptr`, but it seems to cause undefined behavior when I attempt to resume the coroutine. Here's a simplified version of my code: ```cpp #include <iostream> #include <memory> #include <coroutine> struct Awaiter { bool await_ready() const noexcept { return false; } void await_suspend(std::coroutine_handle<>) noexcept {} void await_resume() noexcept {} }; std::shared_ptr<int> my_coroutine() { co_return std::make_shared<int>(42); } int main() { auto coroutine_lambda = [](std::shared_ptr<int> ptr) -> Awaiter { auto result = my_coroutine(); // The following line seems to cause issues. std::cout << "Value: " << *result << std::endl; co_return; }; auto ptr = std::make_shared<int>(100); coroutine_lambda(ptr); } ``` When I run this code, I get a crash with a segmentation fault. I've tried ensuring the `std::shared_ptr` is alive throughout the lambda execution, but it seems like thereβs something off with the coroutine's lifecycle management. I've also attempted to use `std::move` with the `std::shared_ptr`, but that didn't resolve the issue either. The crash occurs right at the line where I attempt to dereference `result`. Any insights into why this might be happening? Am I mismanaging the coroutines or the pointers somehow? I'm compiling with g++ 10.2.0 and using the `-std=c++20` flag. Any help would be greatly appreciated! My development environment is Windows. Am I missing something obvious? Has anyone else encountered this? The project is a microservice built with C++. Has anyone else encountered this?