Skip to content

HSLL175848494/MemoryTracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Memory Tracer (HSLL::MemoryTracer)

A lightweight C++ memory leak detection tool for tracking memory allocations and capturing stack traces to help identify memory leak issues.

Features

  • Tracks all new/delete operations 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)

Usage Instructions

Compilation Configuration (CMake Example)

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])

Platform-Specific Linking Requirements

  • Windows: Requires linking dbghelp.lib
  • Linux: Requires linking -ldl library, recommended to use -rdynamic compile flag

Code Examples

Basic Usage Pattern

// 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;

Usage Notes

  • StartTracing() and EndTracing() 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

Output Example

================================================================================
                    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
================================================================================

About

A lightweight C++ memory leak detection tool for tracking memory allocations and capturing stack traces to help identify memory leak issues.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages