From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D43413983C20; Mon, 28 Jun 2021 13:37:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D43413983C20 From: "victor.burckel at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/101243] New: Coroutine lambda capture is destroyed twice Date: Mon, 28 Jun 2021 13:37:01 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: victor.burckel at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Jun 2021 13:37:01 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101243 Bug ID: 101243 Summary: Coroutine lambda capture is destroyed twice Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: victor.burckel at gmail dot com Target Milestone: --- In a corourtine expecting a std::function parameter, passing a temporary lambda with a capture results in the capture being destroyed twic= e: See it on gldbolt, gcc vs clang outputs https://godbolt.org/z/jx9Yh5cqM #include #include #include #ifdef __clang__ namespace std::experimental { using std::coroutine_handle; using std::coroutine_traits; } // namespace std::experimental #endif struct task { struct promise_type; using handle_type =3D std::coroutine_handle; struct promise_type { auto initial_suspend() noexcept { return std::suspend_always{}; } auto final_suspend() noexcept { return std::suspend_always{}; } void return_void() noexcept {} task get_return_object() noexcept { return task{handle_type::from_promise(*this)}; } void unhandled_exception() noexcept {} }; task(handle_type h) : h{h} {} ~task() { if (h) { fmt::print("destroying coroutine\n"); h.destroy(); fmt::print("coroutine destroyed\n"); } } bool await_ready() const noexcept { return false; } bool await_suspend(std::coroutine_handle<>) noexcept { h.resume(); return true; } void await_resume() noexcept {} void resume() { h.resume(); } handle_type h; }; struct S { S() { fmt::print("S::S()\n"); } S(const S&) { fmt::print("S::S(const S&)\n"); } S(S&&) { fmt::print("S::S(S&&)\n"); } ~S() { fmt::print("S::~S()\n"); } }; task g(std::function) { fmt::print("in g()\n"); co_return; } task f() { fmt::print("calling g()\n"); co_await g([s=3DS{}] {}); fmt::print("g called\n"); } int main() { auto task =3D f(); fmt::print("resuming f\n"); task.resume(); fmt::print("resuming f\n"); task.resume(); } Gcc outputs: resuming f calling g() S::S() S::S(S&&) in g() resuming f destroying coroutine S::~S() coroutine destroyed S::~S() S::~S() g called destroying coroutine coroutine destroyed While clang outputs: resuming f calling g() S::S() S::S(S&&) in g() resuming f destroying coroutine S::~S() coroutine destroyed S::~S() g called destroying coroutine coroutine destroyed=