UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RefCountedObject.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include <atomic>
5#include "AutoRTFM.h"
7
8namespace Chaos
9{
10
11// Chaos ref-counted object
12// * @note In AutoRTFM, the return value of AddRef/Release may be higher than expected, because the refcount won't decrease
13// until the transaction is committed. This is fine for use with TRefCountPtr, as it doesn't use the refcount directly.
15{
16public:
19 {
20 // We want to report an error if we attempt to destroy a ref-counted object with leaked references.
21 // Sometimes, these objects exist ephemerally on the stack. If so, it should have a refcount of zero right now.
22 if (GetRefCount() != 0)
23 {
24 // If not, it might still have references that are queued up to Release at ONCOMMIT time. So, we check a second
25 // time during OnCommit. (If an ephemeral stack object is destroyed a non-zero refcount, this is user error;
26 // we might report this by check-failing here with a garbage value for its refcount, as its stack representation
27 // might already be overwritten.)
29 {
30 check(GetRefCount() == 0);
31 };
32 }
33 }
36
37 uint32 AddRef() const
38 {
40
42 {
43 bIsFirstReference = (++NumRefs == 1);
44 };
45 UE_AUTORTFM_ONABORT(=, this)
46 {
48 {
49 // We took the first reference, and then aborted. This should undo the taking of
50 // the reference, but shouldn't delete the object if it is transient.
51 --NumRefs;
52 }
53 else
54 {
55 // After an object gains its initial reference, an AddRef call can be balanced
56 // out with a matching Release.
57 Release();
58 }
59 };
60 // TRefCountPtr doesn't use the return value.
61 return 0;
62 }
63
65 {
67 {
68 if (--NumRefs == 0 && RefCountMode == ERCM_Transient)
69 {
70 delete this;
71 }
72 };
73 // TRefCountPtr doesn't use the return value.
74 return 0;
75 }
76
78 {
79 uint32 Ret;
81 {
82 Ret = uint32(NumRefs.load());
83 };
84 return Ret;
85 }
86
87 void MakePersistent() const
88 {
89 ERCM_RefCountMode OriginalMode = RefCountMode;
90
92 {
93 RefCountMode = ERCM_Persistent;
94 };
95 UE_AUTORTFM_ONABORT(=, this)
96 {
97 RefCountMode = OriginalMode;
98 };
99 }
100
101private:
102 // Number of refs onto the object.
103 mutable std::atomic<int32> NumRefs = 0;
104
105 enum ERCM_RefCountMode {
106 // An object is considered Transient by default.
107 // After an initial AddRef, when the reference count reaches zero, it automatically deletes itself.
108 ERCM_Transient,
109 // Calling MakePersistent will convert an object to Persistent.
110 // A Persistent object no longer deletes itself when the reference count reaches zero; the caller
111 // is responsible for deletion. (Basically, this opts out of the reference-counting mechanism.)
112 ERCM_Persistent,
113 };
114 mutable std::atomic<ERCM_RefCountMode> RefCountMode = ERCM_Transient;
115};
116
117} // namespace Chaos
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition RefCountedObject.h:15
uint32 Release() const
Definition RefCountedObject.h:64
FChaosRefCountedObject()
Definition RefCountedObject.h:17
FChaosRefCountedObject(const FChaosRefCountedObject &Rhs)=delete
void MakePersistent() const
Definition RefCountedObject.h:87
uint32 AddRef() const
Definition RefCountedObject.h:37
virtual ~FChaosRefCountedObject()
Definition RefCountedObject.h:18
FChaosRefCountedObject & operator=(const FChaosRefCountedObject &Rhs)=delete
uint32 GetRefCount() const
Definition RefCountedObject.h:77
Definition SkeletalMeshComponent.h:307