From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 2AC573857C4A; Sat, 6 Nov 2021 05:07:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2AC573857C4A From: "pigman46 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/98401] coroutines: Temporaries passed to co_await sometimes cause an extraneous call to destructor at incorrect address Date: Sat, 06 Nov 2021 05:07:56 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: pigman46 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: cc Message-ID: In-Reply-To: References: 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: Sat, 06 Nov 2021 05:07:57 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98401 Michael Theall changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |pigman46 at gmail dot com --- Comment #5 from Michael Theall --- This is also happening for me. In my case i have a promise_type defined by coroutine_traits. This promise_= type contains only a shared_ptr, which is constructed via make_shared on default= and move constructors, and the copy constructor is deleted. a promise_type x is default-constructed and then immediately another promise_type y is move-constructed from x but with the wrong reference. I'll call it x'. promise_type contains a shared_ptr which has the same reference= in both x and x' although x' is never constructed, and the shared_ptr has a refcount of 1. So now we have x with its original shared_ptr 'a'. We have x' with some unspecified shared_ptr 'b' (it was swapped or destroyed when moved to y). We have y also with shared_ptr 'a'. The refcount of 'a' at this point is still 1. Then, by the time my coroutine returns, all of x, x', and y are destructed, which means shared_ptr 'a' is destructed too many times. shared_ptr 'b' is fine. If I assign the awaitable to a local variable and then co_await that instea= d, the problem does not occur. In no cases does the problem occur with clang. = Same behavior on GCC 11.2.1 x86_64 with -std=3Dc++20 and GCC 11.1.0 aarch64 with -std=3Dgnu++20. The problem occurs regardless of optimization level and whe= ther LTO is enabled, if that makes any difference.=