Each level changes every 30 seconds.
If I call ActivateItem() and set the timer with SetTimer() when there are less than 2 seconds left in this timeout, I will get an error
Moving on to the next level
Error: Read access error (?) in Particle->DestroyComponent() in SetTimer()
Why is this happening?
Why is if(IsValid(Particle)) in the lambda function true?
regardless of whether the particle is a nullptr or not.
raw pointer is GC’d, so I expect it to be False
i saw
IsValid() checks, if an object is non-null and not marked ready for being garbage collected during the next garbage collection cycle.
I looked up raw pointers and GCs and TWeakObjectPtrs and stuff like that, but I have no idea why that code doesn’t work.
Please let me know what I’m missing, even if it’s something small.
The issue you’re experiencing is related to how lambda functions capture variables in C++ and how pointers work. When capturing raw pointers in a lambda, you’re storing the pointer’s value at the time the lambda is created. However, if the object is destroyed (e.g., due to a map change), the pointer will still reference the same memory address, but the object will no longer exist—leading to a crash when accessed.
IsValid will return true because the pointer itself isn’t null (since it’s capturing the original value), but the underlying object may be gone. I strongly recommend avoiding raw pointers for lambda functions and bindings like this. Instead, use TWeakObjectPtr, which integrates with Unreal’s reflection and garbage collection system to properly track whether the object has been destroyed.