UE4: Guidebook
4.24
4.24
  • Introduction
  • Quick Reference
    • C++ Data Type Snippets
    • The UPROPERTY Macro
    • The UFUNCTION Macro
  • Wiki Archives
    • Debugging & Utilities
      • Exec Functions
      • How To Prevent Crashes Due To Dangling Actor Pointers
      • Profiling: How to Count CPU Cycles
      • Logs: Printing Messages to Yourself during Runtime
    • Macros & Data Types
      • Structs, USTRUCTS(), They're Awesome
      • Enums For Both C++ and BP
      • Delegates in UE4, Raw C++, and BP Exposed
      • Interfaces in C++
      • Iterators
      • String Conversions: FString to FName, FString to Int32, Float to FString
    • Networking
      • Standalone Dedicated Server
      • How To Use Sessions In C++
      • Spawn Different Pawns For Players in Multiplayer
      • Spawn Different Pawns For Every Player
      • Gameplay Abilities and You
    • DevOps
      • Linking DLLs
    • AR & VR
      • Integrating OpenCV into Unreal Engine 4
Powered by GitBook
On this page
  • UPROPERTY() UObjects Clear References Properly
  • TWeakObjectPtr
  • Conclusion

Was this helpful?

  1. Wiki Archives
  2. Debugging & Utilities

How To Prevent Crashes Due To Dangling Actor Pointers

This wiki article was written by Rama.

PreviousExec FunctionsNextProfiling: How to Count CPU Cycles

Last updated 5 years ago

Was this helpful?

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<AActor*> 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() //<~~~ That's it! This now makes the pointers much more stable! -Rama
TArray<AActor*> UnitArray;

TWeakObjectPtr

For UObjects especially, having lots of UPROPERTY() references to them can prevent them from getting garbage collected properly. For this situation you can use TWeakObjectPtr which will still give you additional validity option using IsValid() but will not prevent GC from running.

Conclusion

If you are encountering AActor* pointers that are going stale and crashing your game, make sure they are marked with UPROPERTY() and you will be taking advantage of a rather essential feature of UObjects in UE4, which is that all UPROPERTY() references get updated to NULL when a UObject is destroyed.

Have fun today!

Rama

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Objects/Optimizations/index.html#automaticupdatingofreferences