Memory Leaks
A memory leak occurs when heap memory is allocated but never freed — the program loses its pointer while memory stays reserved.
Introduction
A memory leak occurs when heap memory is allocated but never freed — the program loses its pointer while memory stays reserved. Pair AddressSanitizer (ASan/LSan) and Valgrind with disciplined error-path cleanup to eliminate leaks, double-frees, and buffer overflows.
Understanding the topic
Prevention & error-path cleanup Every allocation has one clear owner; use single-exit goto cleanup blocks so early error returns never skip free().
- AddressSanitizer (ASan + LSan + UBSan) — Fastest memory debugger during development (~2x slowdown).
- Valgrind Memcheck — No recompile required (compile with -g).
- Prevention & error-path cleanup — Every allocation has one clear owner; use single-exit goto cleanup blocks so early error returns never skip free().
Step-by-step explanation
- AddressSanitizer (ASan + LSan + UBSan) — Fastest memory debugger during development (~2x slowdown).
- Valgrind Memcheck — No recompile required (compile with -g).
- Prevention & error-path cleanup — Every allocation has one clear owner; use single-exit goto cleanup blocks so early error returns never skip free().
Informative example
Example program:
#include <stdlib.h>int safe_process(size_t n) {int *buf = calloc(n, sizeof *buf);if (!buf) return -1;/* ... work on buf[0..n-1] ... */free(buf);return 0;}
Execution workflow
AddressSanitizer (ASan + LSan + UBSan)
Fastest memory debugger during development (~2x slowdown).
Code examples
AddressSanitizer (ASan + LSan + UBSan)
Fastest memory debugger during development (~2x slowdown). Catches heap/stack buffer overflows, use-after-free, double-free, and leaks.
gcc -std=c17 -Wall -Wextra -g -O1 -fsanitize=address,undefined app.c -o app./app
Valgrind Memcheck
No recompile required (compile with -g). Reports definitely lost blocks, invalid reads/writes, and uninitialized value origins on Linux.
gcc -std=c17 -Wall -Wextra -g app.c -o appvalgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./app
Best practices
- Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
- Give every variable a defined value before it is read.
- Stay inside array bounds — C will not stop you from over-running a buffer.
Common mistakes
- Reading uninitialized storage — behavior is undefined.
- Dismissing compiler warnings instead of fixing root causes.
- Ignoring NULL returns from malloc, fopen, and similar APIs.
Hands-on exercise
Practice problems:
- Exercise 1 (ASan): Introduce a 1-byte heap-buffer-overflow, compile with -fsanitize=address, and fix it from the stack trace
- Exercise 2 (Valgrind): Run valgrind --leak-check=full on a program with an early return before free() and fix all 'definitely lost' bytes
- Exercise 3 (Use-after-free): Catch a dangling pointer read after free() with ASan and neutralize it by setting the pointer to NULL (C23: nullptr)
Summary
Memory Leaks in C — Orphaned allocations and how to detect them.