public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101243] New: Coroutine lambda capture is destroyed twice
@ 2021-06-28 13:37 victor.burckel at gmail dot com
  2021-06-28 13:37 ` [Bug c++/101243] " victor.burckel at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: victor.burckel at gmail dot com @ 2021-06-28 13:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101243

            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<void()> parameter, passing a
temporary lambda with a capture results in the capture being destroyed twice:
See it on gldbolt, gcc vs clang outputs
https://godbolt.org/z/jx9Yh5cqM

#include <fmt/core.h>

#include <coroutine>
#include <functional>

#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 = std::coroutine_handle<promise_type>;

  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<void()>) {
  fmt::print("in g()\n");
  co_return;
}

task f() {
  fmt::print("calling g()\n");
  co_await g([s=S{}] {});
  fmt::print("g called\n");
}

int main() {
  auto task = 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-10-01 20:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-28 13:37 [Bug c++/101243] New: Coroutine lambda capture is destroyed twice victor.burckel at gmail dot com
2021-06-28 13:37 ` [Bug c++/101243] " victor.burckel at gmail dot com
2021-06-28 18:57 ` iains at gcc dot gnu.org
2021-08-26 11:39 ` nathan at gcc dot gnu.org
2021-08-26 11:49 ` redi at gcc dot gnu.org
2021-08-26 23:14 ` redi at gcc dot gnu.org
2021-10-01 20:37 ` iains at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).