TWeakPtr
#include "Templates/SharedPointer.h" Description
A non-intrusive weak reference to an object managed by TSharedPtr/TSharedRef. Does not contribute to the reference count and becomes invalid when all strong references are gone.
Signature
template<class ObjectType, ESPMode InMode> class TWeakPtr Caveats & Gotchas
- • TWeakPtr is for non-UObject types only. For UObject weak references use TWeakObjectPtr.
- • To use the pointer, call Pin() to get a TSharedPtr — this atomically checks validity and creates a strong reference.
- • Not thread-safe by default. Use ESPMode::ThreadSafe if shared across threads.
Example
Observer pattern with TWeakPtr C++
TWeakPtr<FMySystem> WeakSystem;
TSharedPtr<FMySystem> System = MakeShared<FMySystem>();
WeakSystem = System;
// Later, after System may have been released:
if (TSharedPtr<FMySystem> Pinned = WeakSystem.Pin())
{
Pinned->DoWork();
} See Also
Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?