Smart Pointers & Memory in Unreal

🛡️ Smart Pointers & Memory Management in Unreal Engine 5

Unreal Engine uses two parallel memory systems:

  1. Garbage Collected (GC) UObjects: Managed by Unreal's reflection system (UCLASS(), UPROPERTY()).
  2. C++ Non-UObject Smart Pointers: Used for standard C++ classes, math buffers, and network protocols.

🧱 1. TObjectPtr vs TSoftObjectPtr (UObjects)


⚡ 2. Non-UObject Smart Pointers (TSharedPtr, TSharedRef, TWeakPtr)

Pointer Ownership Nullable Best Used For
TSharedRef<T> Shared (Ref Counted) Cannot be null Guarantees instance exists (UI widgets, Slate)
TSharedPtr<T> Shared (Ref Counted) Yes (nullptr) Shared non-UObject data
TWeakPtr<T> Non-owning Yes (IsValid()) Breaking circular references
TUniquePtr<T> Exclusive ownership Yes Scoped non-copyable heap objects