detecting-memory-leaks

majiayu000's avatarfrom majiayu000

Detects memory leak vulnerabilities by identifying unfreed memory allocations and missing cleanup in error paths. Use when analyzing long-running processes, resource management, or investigating memory exhaustion issues.

⭐0stars🔀0forks📁View on GitHub🕐Updated Jan 5, 2026

When & Why to Use This Skill

This Claude skill provides automated memory leak detection by identifying unfreed memory allocations and missing cleanup in error paths. It enhances software reliability and security by tracing allocation lifecycles, analyzing exception handling, and identifying reference cycles in complex codebases, making it an essential tool for maintaining high-performance, long-running applications.

Use Cases

  • Analyzing long-running server processes to identify and fix gradual memory exhaustion that leads to system instability.
  • Conducting security audits to detect memory-related vulnerabilities that could be exploited for Denial of Service (DoS) attacks.
  • Automating code reviews for C/C++ projects to ensure every malloc/new call has a corresponding free/delete, especially within complex error-handling blocks.
  • Investigating resource management issues in embedded systems where memory is limited and leaks can cause immediate hardware failure.
  • Tracing complex reference cycles and shared pointer mismanagement in large-scale object-oriented applications.
namedetecting-memory-leaks
descriptionDetects memory leak vulnerabilities by identifying unfreed memory allocations and missing cleanup in error paths. Use when analyzing long-running processes, resource management, or investigating memory exhaustion issues.

Memory Leak Detection

Detection Workflow

  1. Identify allocation points: Find all malloc/calloc/realloc calls, new/delete pairs (C++), map allocation locations
  2. Trace allocation lifecycle: Use xrefs_to to trace allocations, follow pointers through code, identify all free() calls
  3. Check cleanup paths: Verify every allocation has a corresponding free, check all error handling paths, review exception handling, assess callback cleanup
  4. Assess impact: How much memory is leaked? How often does it leak? What's the cumulative impact? Can it cause DoS?

Key Patterns

  • Missing free: malloc() without corresponding free(), calloc() without free(), new without delete (C++)
  • Exception path leaks: allocations before exception-prone operations, missing cleanup in error handling, leaks in early returns
  • Reference cycles: circular references in data structures, reference counting bugs, shared pointer cycles
  • Long-lived allocations: allocations that never get freed, caches without size limits, accumulating data structures

Output Format

Report with: id, type, subtype, severity, confidence, location, allocation (function, address, size), leak path, leak scenario, leak size per call, frequency, exploitability, impact, mitigation.

Severity Guidelines

  • HIGH: Large leaks in frequently called functions
  • MEDIUM: Small leaks or infrequent allocations
  • LOW: Minor leaks with limited impact

See Also

  • patterns.md - Detailed detection patterns and exploitation scenarios
  • examples.md - Example analysis cases and code samples
  • references.md - CWE references and mitigation strategies