I made a small example program that uses the linker to wrap custom functions around malloc and free.
It keeps track of all alocations in a resizeable array and provides a function
- Code: Select all
It doesn't double-free anything and doesn't leak any memory from my tests.
It uses "-Wl,-wrap=malloc -Wl,-wrap=free" as LDFLAGS.
It also seems to work with smart pointers and new.
For now it searches the entire array for a free place or for the entry to free.
I'm sure you could optimise this by making separate buckets of allocations depending on the address.
Should I optimize this and make it into a library?