C++20中调用coroutine_handle.destroy()时协程崩溃的问题咨询
C++20中调用coroutine_handle.destroy()时协程崩溃的问题咨询
大家好,我目前在使用C++20搭配MSVC 2022实现一个简单的自定义协程结构,但遇到了一个崩溃问题:当SimpleCoroutine对象被析构、执行coroutine_handle.destroy()调用时,程序会直接崩溃。我初步怀疑这和协程的生命周期管理或者句柄的处理逻辑有关,想请大家帮忙排查一下问题出在哪里。
先贴出我的完整实现代码:
#include <iostream> #include <coroutine> #include <thread> #include <chrono> #include <spdlog/spdlog.h> struct SimpleCoroutinePromise; struct SimpleCoroutine { using promise_type = SimpleCoroutinePromise; std::coroutine_handle<promise_type> handle; SimpleCoroutine(std::coroutine_handle<promise_type> handle) : handle(handle) {} SimpleCoroutine(const SimpleCoroutine&) = delete; SimpleCoroutine& operator=(const SimpleCoroutine&) = delete; SimpleCoroutine(SimpleCoroutine&& other) noexcept : handle(other.handle) { other.handle = nullptr; } SimpleCoroutine& operator=(SimpleCoroutine&& other) noexcept { if (this != &other) { if (handle) { handle.destroy(); // Destroy the current handle if it exists } handle = other.handle; other.handle = nullptr; } return *this; } void resume() { if (handle) { handle.resume(); } } ~SimpleCoroutine() { if (handle) { handle.destroy(); } } }; struct SimpleCoroutinePromise { SimpleCoroutine get_return_object() { SPDLOG_INFO("get_return_object"); return SimpleCoroutine(std::coroutine_handle<SimpleCoroutinePromise>::from_promise(*this)); } void return_void() { SPDLOG_INFO("return_void"); } std::suspend_always initial_suspend() noexcept { SPDLOG_INFO("initial_suspend"); return {}; } std::suspend_never final_suspend() noexcept { SPDLOG_INFO("final_suspend"); return {}; } void unhandled_exception() { SPDLOG_INFO("unhandled_exception"); } }; SimpleCoroutine MySimpleCoroutine() { SPDLOG_INFO("Coroutine Start"); co_return; // This will directly return, and the coroutine ends here SPDLOG_INFO("Coroutine End"); // This line will not be executed } int testSimpleCorontine() { SPDLOG_INFO("Main thread started executing 1"); auto coro = MySimpleCoroutine(); SPDLOG_INFO("Main thread started executing 2"); coro.resume(); return 0; } int main(int argc, char **argv){ SPDLOG_INFO("Hello, World!"); testSimpleCorontine(); return 0; }
程序运行时的日志输出如下:
[2025-07-23 21:49:03.846] [info] [main.cpp:5] Hello, World! [2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:78] Main thread started executing 1 [2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:48] get_return_object [2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:57] initial_suspend [2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:80] Main thread started executing 2 [2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:72] Coroutine Start [2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:53] return_void [2025-07-23 21:49:03.848] [info] [SimpleCorontine.cpp:62] final_suspend
崩溃精准发生在SimpleCoroutine析构函数调用handle.destroy()的环节。我注意到自己在SimpleCoroutinePromise里设置final_suspend返回的是std::suspend_never,会不会是因为这个配置,协程在执行完final_suspend后会自动销毁协程帧,导致我后续调用destroy()时,其实是在操作已经被释放的内存,进而触发崩溃?
如果是这个原因的话,是不是应该让final_suspend返回std::suspend_always,把协程帧的销毁权完全交给用户手动通过handle.destroy()管理?或者我的句柄转移、销毁的逻辑里还有其他疏漏?麻烦大家帮忙分析一下,非常感谢!
内容来源于stack exchange
相关产品推荐
相关产品推荐

