[Question] About TCoroutine lifetime
ameaninglessname opened this issue · 1 comments
ameaninglessname commented
I want to start introducing coroutine to my project by using it to solve one replication race condition:
void CallBack()
{
// normal function part
auto Coro = [Pawn]() -> UE5Coro::TCoroutine<>
{
ON_SCOPE_EXIT
{
UE_LOG(LogTemp, Log, TEXT("I am dead")); // (1
};
// wait until Pawn has a valid PlayerState
const auto* PlayerState = co_await Coro_GetPlayerStateFromPawn(Pawn); // (2
// do stuff with PlayerState
co_return;
};
Coro(); // (3
}
Q1: Why the (1
part not executed after Coro();
the object that Coro()
created should get out of scope and destructed, so the related coroutine frame also get destroyed?
("out of scope but can still run" is what a want though, I noticed it was turning into a latent action in FLatentAwaiter::Suspend
)
Q2 How to not executing (2 when PIE exit?
I found that if I can't get the PlayerState until PIE exit, the coroutine is resumed to (2
,
so I need to check PlayerState
to prevent crash, but I hope it do not run in this situation.
landelare commented
Not a bug. TCoroutines don't own the coroutine and your lambda is not latent, therefore it cannot use Latent::Cancel.