C26811

warning C26811: Lifetime of the memory referenced by parameter <var> might end by the time the coroutine is resumed.

Warning C26811 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 C26811.


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

using namespace std::experimental;

// 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() {}
};

coroutine_handle<> g_suspended_coro;

std::future<void> async_coro(int &a)
{
  co_await ManualControl{g_suspended_coro};   // @expected(26811), Lifetime of 'a' might end by the time this coroutine is resumed.
  ++a;
}

See also