跟踪内存泄漏时如何获取stacktrace?
我用 C++ 编写了一个内存跟踪系统,使用 Detours 来修补各种内存分配函数。当我收到对 malloc 的调用时,除了 malloc 之外,我还存储堆栈跟踪(这样我就可以查明泄漏)。
获得准确堆栈跟踪的唯一可靠方法是使用 StackWalk64(我尝试了 RtlCaptureStackBackTrace,这只能捕获非常简单的堆栈)。
然而,这是我的问题,StackWalk64 调用 malloc,而 malloc 又调用 StackWalk64 并导致堆栈溢出。现在我可以有一个处理递归调用的标志,但这不适用于多个线程,
我想知道是否有人对这个泡菜有可能的解决方案。
谢谢 里奇·卡莱斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 malloc 实现中使用线程本地标志来防止对 StackWalk64 的递归调用吗?
Could you use a thread-local flag in your malloc implementation to prevent the recursive calls to StackWalk64?
我们曾经遇到过类似的问题,并通过将调试打印代码预链接到另一个(修改后的)版本的 malloc 来解决它,该版本取自 glibc 并稍作修改以在预分配的缓冲区上运行(我们希望避免对操作系统的任何内存活动)在我们的例子中)。
不过,我无法判断您的系统中的静态预链接有多困难。
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.