경고 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