landelare / ue5coro

A C++20 coroutine plugin offering seamless integration with Unreal Engine 5.
BSD 3-Clause Clear License
617 stars 57 forks source link

[Question] Why the lambda object need to be copied to heap? #6

Closed ameaninglessname closed 1 year ago

ameaninglessname commented 1 year ago

auto* Copy = new std::function(std::move(Fn));

Why not just Fn(...) to get the co-routine object?

landelare commented 1 year ago

Because in that case the coroutine would be resumed after its this (the lambda object) gets destroyed.

The "why not" version breaks the UE5Coro.Aggregate.* tests in Debug Editor¹, it just gets lucky with how undefined behavior plays out in other configurations (including DebugGame Editor).

This line is in a private test helper that does not affect your project anyway.

¹UE 5.1.0, MSVC 19.34.31937.0, Windows SDK 10.0.22621.755.