TWeakObjectPtr
#include "UObject/WeakObjectPtr.h" Description
A weak reference to a UObject that does not prevent garbage collection. Automatically becomes invalid when the referenced object is destroyed.
Signature
template<class T, class TWeakObjectPtrBase> struct TWeakObjectPtr Caveats & Gotchas
- • Always call IsValid() before dereferencing — the object may have been GC'd since you stored the pointer.
- • TWeakObjectPtr cannot be used across frame boundaries safely without the IsValid() check each time.
- • Serialization: TWeakObjectPtr is not saved/loaded automatically. Use TSoftObjectPtr if you need a persistent reference.
- • Can be used as a UPROPERTY — unlike raw pointers, it will not keep the object alive.
Example
Store a reference that shouldn't block GC C++
TWeakObjectPtr<AActor> WeakTarget;
// Store
WeakTarget = SomeActor;
// Use later
if (WeakTarget.IsValid())
{
WeakTarget->DoSomething();
} See Also
Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?