SetTimer with lambda execution safety

Hi guys

I am using following code to delay the execution of my code (i.e. blueprint-like Delay in C++):

As you can see, lambda does some operations with properties and methods in this object (which is scope-captured by β€œ=”). Will this crash, if e.g. this (AActor in which this code is written) is destroyed after setting timer on the last line of this code, but before timer fires?

I have the same Issue in ue4.21.

I’ve managed to avoid crashes by explicitly making a TWeakObjectPtr of β€œthis” and capturing that ptr in the lambda scope (by value). Inside lambda I check if that weakPtr.isValid() and have not faced any crashes so far.
Pls lemme know if that works for u.

1 Like

TWeakObjectPtr is the save way to do this,
generally just using this in lambdas that are not immediately executed is a bad idea