How do memory leak detection tools like purify and valgrind work?
How can I design and implement my own such tool?
Such tools usually instrument the exeutable with their own code. for instance they replace every call to malloc()
and free()
with their own functions which allows them to follow every allocation.
In Visual Studio this can be done automatically using just the C Runtime Library using functions from the family of _CrtDumpMemoryLeaks()
For basic leak detection you just need to hook into low level memory allocation routines, e.g. by patching malloc/free. You then track all allocations and subsequently report any that have not been freed at an appropriate point, e.g. just prior to exit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With