A small C library and educational resource for detecting memory leaks, double-free/invalid-free, and tracking heap usage. Includes:
- A wrapper around
malloc/calloc/realloc/freethat records allocations at runtime. - A demo program showing proper frees, leaks, and invalid/double frees.
- A run script (
run.sh) to inject the tracker header, build viamake, run the executable, and display the leak report. - Detailed documentation on why leaks happen and how to use the wrapper.
src/leak_tracker.h&leak_tracker.c: the tracking library.
examples/demo_allocs.c: a minimal demo that intentionally leaks memory and does invalid/double frees.
run.sh- Shell script that:
- Looks for
<your_file>.cinexamples/orsrc/ - Prepends
#include "leak_tracker.h" - Creates
main.cin the root - Invokes
maketo compile - Runs
leak_test_execto show leak diagnostics - Cleans up all generated files afterward
- Looks for
- Shell script that:
Makefile- Builds
main.c+src/leak_tracker.cintoleak_test_execwhen you runmake.
- Builds
docs/memory_leaks.md: conceptual overview of memory leaks (what they are, why they happen).using_wrapper.md: step-by-step on how to build and use the leak tracker withrun.sh.
-
Clone or download this repo:
git clone https://github.com/erer-can/memory-leak-tracker cd memory-leak-tracker -
Run the demo:
chmod +x run.sh ./run.sh examples/demo_allocs.c
You should see output from
demo_allocs.c, followed by warnings (if any) and a final memory-leak report. -
Use in your own project:
- Copy
src/leak_tracker.handsrc/leak_tracker.cinto your C project. - Place your C file (e.g.,
my_program.c) inexamples/orsrc/. - Run
./run.sh my_program.c; it will automatically:- Inject
#include "leak_tracker.h" - Build via
make - Run the executable with leak diagnostics
- Clean up generated files
- Inject
- Copy
See docs/memory_leaks.md for an in-depth explanation of what memory leaks are, common causes, and how they can crash or degrade long-running services.
- The script
run.shprepends#include "leak_tracker.h"to your source, creatingmain.c. - The
Makefilecompiles:main.c→main.osrc/leak_tracker.c→src/leak_tracker.o- Links into
leak_test_exec.
- At runtime,
leak_tracker.hredefines:so each allocation or free is recorded along with file/line metadata.#define malloc(sz) my_malloc((sz), __FILE__, __LINE__) #define calloc(nm, s) my_calloc((nm), (s), __FILE__, __LINE__) #define realloc(p, s) my_realloc((p), (s), __FILE__, __LINE__) #define free(p) my_free((p), __FILE__, __LINE__)
- If you call
freeon an untracked pointer, the wrapper prints a warning. - On program exit, an
atexit()handler walks the list of active allocations—anything still unfreed is printed in the final leak report.
For full details, see docs/using_wrapper.md.
This repo uses a simple script called run.sh to automate everything.
To use the memory leak tracker on an example program:
chmod +x run.sh
./run.sh demo_allocs.cThis script will:
- Automatically inject the tracking header
- Build the project using
make - Run the program and show leak diagnostics
- Clean up all generated files afterward
This repository was developed as a teaching aid for the CMPE230 Systems Programming course at Boğaziçi University.
Feel free to explore the code for educational purposes.