Let’s say you would like to handle an arbitrary event with a lambda defined as following:
auto Func = [this](){ CallingFunctionOnThis(); }Now, what happens is this is destroyed but the lambda is not removed from the delegate? Most likely thing to happen is that the game will crash.
Here is a safe way to do it using MakeWeakObjectPtr function:
auto Func = [WeakThis = MakeWeakObjectPtr(this)]()
{
if (WeakThis.IsValid)
{
WeakThis->CallingFunctionOnThis();
}
};
