RealDocs

TSharedPtr::SharedReferenceCount

property Core Since 4.0
#include "Templates/SharedPointer.h"
Access: private

Description

The reference-counter handle for the managed object. Shared by all TSharedPtr and TSharedRef instances that point to the same object. Use GetSharedReferenceCount() for debugging; do not access this field directly.

Caveats & Gotchas

  • SharedReferenceCount is private. The public GetSharedReferenceCount() method wraps it and is clearly labelled 'not necessarily fast — use for debugging only'. Avoid calling it in hot paths or for game logic.
  • The reference controller pointed to by SharedReferenceCount is shared with any TWeakPtr referencing the same object. The controller is only freed after both the shared count and the weak count reach zero.
  • In thread-safe mode (ESPMode::ThreadSafe), the counter uses atomic operations. In ESPMode::NotThreadSafe mode it uses plain integer arithmetic — the modes are not interchangeable.

Signature

SharedPointerInternals::FSharedReferencer< Mode > SharedReferenceCount

Example

Checking reference count for debugging C++
TSharedPtr<FMyData> A = MakeShared<FMyData>();
TSharedPtr<FMyData> B = A; // share ownership

// Debug only — not for production logic:
UE_LOG(LogTemp, Log, TEXT("Ref count: %d"), A.GetSharedReferenceCount()); // 2

B.Reset();
UE_LOG(LogTemp, Log, TEXT("Ref count: %d"), A.GetSharedReferenceCount()); // 1

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.