How to get stacktrace while tracking memory leaks?

I wrote a memory tracking system in C ++ using Detours to fix various memory allocation functions. When I get a call to malloc in addition to malloc, I also save stacktrace (so that I can point to a leak).

The only reliable way to get accurate stacking is to use StackWalk64 (I tried RtlCaptureStackBackTrace, and it only managed to stack very simple stacks).

However, here is my problem, StackWalk64 calls malloc, which in turn calls StackWalk64 and causes the stack to overflow. Now I may have a flag that deals with recursive calls, however this does not work with multiple threads

I was wondering if anyone has a possible solution to this pickle.

Thank you Rich Faceless

+4
source share
2 answers

Can you use local thread in malloc implementation to prevent recursive calls to StackWalk64?

+1
source

We had a similar problem and it was solved by pre-binding the debugging code for another (modified) version of malloc, which was taken from glibc and slightly modified to work in a pre-allocated buffer (we wanted to avoid any memory need for the OS in our case) . I can’t say how difficult it is to use static prebinding on your system.

0
source

All Articles