RealDocs

TWeakPtr

struct Core Since 4.0
#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.

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.

Signature

template<class ObjectType, ESPMode InMode> class TWeakPtr

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();
}

Functions (4)

Utility
4
Access Type Name
public function TWeakPtr::Reset
public function TWeakPtr::GetWeakPtrTypeHash
public function TWeakPtr::UE_REQUIRES (constrained conversion constructor)
public function TWeakPtr::UEOpEquals

Properties

Access Type Name
private property TWeakPtr::Object
private property TWeakPtr::WeakReferenceCount

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.