A lightweight C++ memory leak detection tool for tracking memory allocations and capturing stack traces to help identify memory leak issues.
- Tracks all
new/deleteoperations during tracing sessions - Captures detailed stack traces for each memory allocation
- Groups leaks by call stack for easier analysis
- Thread-safe implementation
- Cross-platform support (Windows/Linux)
Must define HS_ENABLE_TRACING macro at compile time to enable tracing:
# CMake configuration example
target_compile_definitions(${PROGRAM_NAME} PRIVATE HS_ENABLE_TRACING)Must add HS_Leak.h include path to your project to ensure all compilation units use the overloaded memory operations:
target_include_directories(${PROJECT_NAME} PRIVATE [path_to_HS_Leak.h_directory])- Windows: Requires linking
dbghelp.lib - Linux: Requires linking
-ldllibrary, recommended to use-rdynamiccompile flag
// Start tracking memory allocations
HSLL::Utils::MemoryTracer::StartTracing();
// Execute code to be monitored
// ...
// Stop tracking and get leak report
std::string report = HSLL::Utils::MemoryTracer::EndTracing();
// Output memory leak report
std::cout << report << std::endl;StartTracing()andEndTracing()can be called from any thread- When
EndTracing()is called concurrently, only the first call will retrieve the report - The report only includes memory allocations between the two calls
================================================================================
MEMORY LEAK REPORT (GROUPED BY STACK TRACE)
================================================================================
LEAK GROUP 1:
Leak Count: 100 allocations
Total Size: 400 bytes
Average Size: 4 bytes
Stack Hash: 0x73d99140c79de33f
Call Stack:
# 0 HSLL::Utils::StackTraceCapturer::Capture
# 1 HSLL::Utils::MemoryTracer::RecordAllocation
# 2 operator new
# 3 Leak1
# 4 main
# 5 invoke_main
# 6 __scrt_common_main_seh
# 7 __scrt_common_main
# 8 mainCRTStartup
# 9 BaseThreadInitThunk
#10 RtlUserThreadStart
--------------------------------------------------------------------------------
LEAK GROUP 2:
Leak Count: 100 allocations
Total Size: 100 bytes
Average Size: 1 bytes
Stack Hash: 0x4f8e60ec5f5d1175
Call Stack:
# 0 HSLL::Utils::StackTraceCapturer::Capture
# 1 HSLL::Utils::MemoryTracer::RecordAllocation
# 2 operator new
# 3 Leak2
# 4 main
# 5 invoke_main
# 6 __scrt_common_main_seh
# 7 __scrt_common_main
# 8 mainCRTStartup
# 9 BaseThreadInitThunk
#10 RtlUserThreadStart
--------------------------------------------------------------------------------
SUMMARY:
Total leak groups: 2
Total allocations: 200
Total leaked memory: 500 bytes
================================================================================