TSharedPtr
#include "Templates/SharedPointer.h" Description
A non-intrusive reference-counted smart pointer for non-UObject types. The object is destroyed when the last TSharedPtr referencing it is released.
Signature
template<class ObjectType, ESPMode InMode> class TSharedPtr Caveats & Gotchas
- • Do NOT use TSharedPtr for UObjects — use TObjectPtr or TWeakObjectPtr instead. TSharedPtr bypasses the GC and will cause double-frees.
- • Create via MakeShared<T>() (single allocation, preferred) or MakeShareable(new T()) (two allocations, legacy).
- • TSharedPtr can be null. TSharedRef is the non-nullable alternative.
- • Not thread-safe by default. Pass ESPMode::ThreadSafe when sharing across threads.
Example
Create and share a non-UObject resource C++
TSharedPtr<FMyData> Data = MakeShared<FMyData>();
Data->Value = 42;
// Shared with another system
OtherSystem->SetData(Data);
// Automatically destroyed when last shared ptr is released See Also
Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?