开发者

How to obtain stacktrace when tracking memory leaks?

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 v开发者_C百科ery 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜