警告 C26811

參數 ' var ' 所參考記憶體的存留期可能會在協同程式繼續時結束。

備註

警告 C26811 會在變數的存留期在繼續協同程式結束之後使用時觸發。

程式碼分析名稱: COROUTINES_USE_AFTER_FREE_PARAM

範例

下列程式碼會產生 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;
}

若要修正此警告,請考慮依值採用 引數:

std::future<void> async_coro(int a)
{
  co_await ManualControl{g_suspended_coro};
  ++a;
}

或者,當 的存留期保證超過協同程式存留期 a 時,請使用 gsl::suppress 隱藏警告,並記錄程式碼的存留期合約。

另請參閱

C26810