views:

83

answers:

2

Hi,

I've written a memory tracking system in c++ using Detours to patch the various memory allocation functions. When I receive a call to malloc in addition to the malloc I also store the stacktrace (so I can pin point the leak).

The only reliable way to obtain an accurate stacktrace is to use StackWalk64 (I tried RtlCaptureStackBackTrace, and this only managed to caputure very simple stacks).

However here's my problem, StackWalk64 calls malloc, which in turn calls StackWalk64 and leads to a stack overflow. Now I could have a flag that deals with recursive calls, however this doesn't work with multiple threads

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

Thanks Rich Carless

+1  A: 

Could you use a thread-local flag in your malloc implementation to prevent the recursive calls to StackWalk64?

Mark B
Yes, and this has fixed my problem, here's the link I used : http://msdn.microsoft.com/en-us/library/ms686997(v=VS.85).aspx
Rich
A: 

We once had a similar problem and solved it by prelinking the debug-printing code against another (modified) version of malloc, which was taken from glibc and slightly modified to operate on a preallocated buffer (we wanted to avoid any memory acitvity towards the OS in our case). I cannot tell how difficult a static prelinkage is in your system, though.

blabla999