How To Prevent Crashes Due To Dangling Actor Pointers

Overview Author () While working on Abatron, an RTS/FPS hybrid game with tons of character units to keep track of, I created a lot of arrays of Actors: TArray UnitArray; During multiplayer testing ...

Updated over 4 years ago Edit Page Revisions

Overview

Author ()

While working on Abatron, an RTS/FPS hybrid game with tons of character units to keep track of, I created a lot of arrays of Actors:

TArray UnitArray;

During multiplayer testing especially, stale / dangling AActor pointers were causing a lot of crashes!

The problem with stale pointers is that just checking ActorPtr != nullptr is not enough, a stale pointer will return true but wont actually still be pointing to a valid AActor, which is what causes the crash.

UPROPERTY() UObjects Clear References Properly

A less-advertised feature of UObject pointers that are made UPROPERTY() is that they are properly updated to NULL when the object is destroyed, unlike raw pointers like I was using above.

Automatic Updating of UObject References

So the simple solution if you are having issues with dangling / stale actor pointers is to make sure all AActor pointers are marked with UPROPERTY().

UPROPERTY() //