C26810

warning C26810: Lifetime of captured variable <var> might end by the time the coroutine is resumed.

Warning C26810 is triggered when a memory region might be used after it went out of scope in a resumed coroutine.

Example

The following code will generate C26810.


#include <experimental/generator>
#include <future>

using namespace std::experimental;

coroutine_handle<> g_suspended_coro;

// Simple awaiter to allows to resume a suspended coroutine
struct ManualControl
{
  coroutine_handle<>& save_here;

  bool await_ready() { return false; }
  void await_suspend(coroutine_handle<> h) { save_here = h; }
  void await_resume() {}
};

void bad_lambda_example1()
{
  int x = 5;
  auto bad = [x]() -> std::future<void> {
    co_await ManualControl{g_suspended_coro};  // @expected(26810), Lifetime of capture 'x' might end by the time this coroutine is resumed.
    printf("%d\n", x);
  };
  bad();
}

See also