Smart Pointers & Memory in Unreal
🛡️ Smart Pointers & Memory Management in Unreal Engine 5
Unreal Engine uses two parallel memory systems:
- Garbage Collected (GC) UObjects: Managed by Unreal's reflection system (
UCLASS(),UPROPERTY()). - C++ Non-UObject Smart Pointers: Used for standard C++ classes, math buffers, and network protocols.
🧱 1. TObjectPtr vs TSoftObjectPtr (UObjects)
TObjectPtr<T>: The default replacement for rawT*pointers insideUPROPERTY()in UE5. Provides automatic access tracking and dynamic resolution in editor builds.TSoftObjectPtr<T>: Stores a string path (FSoftObjectPath) to an asset on disk without loading it into RAM (see Async Loading vs. Streamable Manager).
⚡ 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 |
🔗 Related Notes
- Index for Unreal — C-style C++ design.
- Async Loading vs. Streamable Manager — Soft pointers and async loading.
- Optimization — TArray memory reservation.