C Memory Police
A VS Code extension that detects potential memory leaks in C code by checking each function for unmatched malloc/calloc allocations.
Features
- Function-level analysis: Checks every function body for
malloc/calloc calls that are never free()d
- Modular project support: Follows
#include "..." directives into custom headers and their .c files to detect allocating functions
- Real-time feedback: Analyzes while you type (500ms debounce) and on save
- Warning diagnostics: Shows yellow squiggly underlines + entries in the Problems panel
- Helpful hover messages: Hover over a warning to see which variable leaked and how to fix it
- Comment-aware: Ignores
malloc/calloc calls inside comments
How it Works
- When you open or edit a
.c file, the extension scans for #include "..." directives
- It parses my custom header's corresponding
.c file to find functions that call malloc/calloc internally
- It parses every function in your file and checks:
- Is there a
malloc() or calloc() call?
- Is the result variable passed to
free() before the function ends?
- Is there a call to a known allocating function whose result is never freed?
- If a leak is found, a yellow warning appears on the allocation line
Usage
Just open a .c file — the extension activates automatically!
Or run the command: C Memory Police: Check Current File from the command palette (Ctrl+Shift+P).
Testing
Open test/test_main.c in the Extension Development Host to see the extension in action. Expected warnings:
leaked_ptr — malloc without free
data — calloc without free
buffer — call to create_buffer() (which internally mallocs) without free
temp in process_data() — malloc without free
| |