UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MallocLeakDetection.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 MallocLeakDetection.h: Helper class to track memory allocations
5=============================================================================*/
6
7#pragma once
8
9#include "Containers/Array.h"
10#include "Containers/Map.h"
11#include "Containers/Set.h"
13#include "CoreTypes.h"
14#include "HAL/MemoryBase.h"
15#include "HAL/ThreadSafeBool.h"
16#include "HAL/UnrealMemory.h"
17#include "Hash/CityHash.h"
18#include "Misc/Crc.h"
19#include "Misc/ScopeLock.h"
20
21#ifndef MALLOC_LEAKDETECTION
22 #define MALLOC_LEAKDETECTION 0
23#endif
24
63
64#if MALLOC_LEAKDETECTION
65
70{
72
73 struct FCallstackTrack
74 {
76 {
77 FMemory::Memzero(this, sizeof(FCallstackTrack));
78 }
79 static constexpr int32 Depth = 32;
80 uint64 Callstack[Depth];
82 uint32 LastFrame;
84 CallstackHashType CachedHash;
86
87 // least square line fit stuff
91 float SumOfMemory;
93
94 // least square line results
95 float Baseline;
96 float BytesPerFrame;
97
98 bool operator==(const FCallstackTrack& Other) const
99 {
100 bool bEqual = true;
101 for (int32 i = 0; i < Depth; ++i)
102 {
103 if (Callstack[i] != Other.Callstack[i])
104 {
105 bEqual = false;
106 break;
107 }
108 }
109 return bEqual;
110 }
111
112 bool operator!=(const FCallstackTrack& Other) const
113 {
114 return !(*this == Other);
115 }
116
117 void GetLinearFit();
118
119 CallstackHashType GetHash()
120 {
121 const char* Data = reinterpret_cast<const char*>(Callstack);
122 CachedHash = CityHash64(Data, sizeof(Callstack));
123 return CachedHash;
124 }
125 };
126
127private:
128
131
135 CORE_API void AddCallstack(FCallstackTrack& Callstack);
137
140
143
146
149
152
154 struct FContextString { TCHAR Buffer[64]; };
155 TArray<FContextString> Contexts;
156
159
161 bool bRecursive;
162
164 bool bCaptureAllocs;
165
167 int32 MinAllocationSize;
168
171
174
175public:
176
178 static CORE_API void HandleMallocLeakCommand(const TArray< FString >& Args);
179
181 CORE_API void SetAllocationCollection(bool bEnabled, int32 Size = 0);
182
184 bool IsAllocationCollectionEnabled(void) const { return bCaptureAllocs; }
185
187 CORE_API void ClearData();
188
191
194
196 CORE_API void Malloc(void* Ptr, SIZE_T Size);
197
199 CORE_API void Realloc(void* OldPtr, SIZE_T OldSize, void* NewPtr, SIZE_T NewSize);
200
202 CORE_API void Free(void* Ptr);
203
206
209
212 void PushContext(const FString& Context)
213 {
214 this->PushContext(*Context);
215 }
216 CORE_API void PushContext(const TCHAR* Context);
217
219 CORE_API void PopContext();
220
223};
224
232{
233public:
235 {
236 FMallocLeakDetection::Get().SetDisabledForThisThread(true);
237 }
238
240 {
241 FMallocLeakDetection::Get().SetDisabledForThisThread(false);
242 }
243
244 // Non-copyable
247};
248
250{
251public:
252 template <typename ArgType>
254 {
255 FMallocLeakDetection::Get().PushContext(Forward<ArgType>(Context));
256 }
257
259 {
260 FMallocLeakDetection::Get().PopContext();
261 }
262
263 // Non-copyable
265 FMallocLeakScopedContext& operator=(const FMallocLeakScopedContext&) = delete;
266};
267
268#define MALLOCLEAK_IGNORE_SCOPE() \
269 FMallocLeakDetectionIgnoreScope ANONYMOUS_VARIABLE(DetectionShouldIgnoreScope)
270
271#define MALLOCLEAK_SCOPED_CONTEXT(Context) \
272 FMallocLeakScopedContext ANONYMOUS_VARIABLE(ScopedContext)(Context)
273
274#else // MALLOC_LEAKDETECTION 0
275
276#define MALLOCLEAK_IGNORE_SCOPE()
277#define MALLOCLEAK_SCOPED_CONTEXT(Context)
278
279#endif // MALLOC_LEAKDETECTION
280
uint64 CityHash64(const char *s, uint32 len)
Definition CityHash.cpp:388
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
UE_FORCEINLINE_HINT bool operator!=(const FIndexedPointer &Other) const
Definition LockFreeList.h:76
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition OutputDevice.h:133
Definition Array.h:670
Definition UnrealString.h.inl:34
FORCEINLINE T * Get(const FObjectPtr &ObjectPtr)
Definition ObjectPtr.h:426
Definition MallocLeakDetection.h:29
uint32 FrameStart
Definition MallocLeakDetection.h:52
FMallocLeakReportOptions()
Definition MallocLeakDetection.h:37
ESortOption SortBy
Definition MallocLeakDetection.h:58
uint32 SizeFilter
Definition MallocLeakDetection.h:43
float RateFilter
Definition MallocLeakDetection.h:46
uint32 FrameEnd
Definition MallocLeakDetection.h:55
ESortOption
Definition MallocLeakDetection.h:31
bool OnlyNonDeleters
Definition MallocLeakDetection.h:49
FOutputDevice * OutputDevice
Definition MallocLeakDetection.h:61
static UE_FORCEINLINE_HINT void * Memzero(void *Dest, SIZE_T Count)
Definition UnrealMemory.h:131