内存泄漏调试 [英] memory leak debug

查看:112
本文介绍了内存泄漏调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


推荐答案

拦截所有功能分配和释放内存(根据平台,列表可能如下所示:malloc,calloc,realloc,strdup,getcwd,free),除了执行这些功能最初的功能外,还可以在某个地方保存有关呼叫的信息动态增长的全局数组可能由多线程程序的同步原语保护。

Intercept all functions that allocate and deallocate memory (depending on the platform, the list may look like: malloc, calloc, realloc, strdup, getcwd, free), and in addition to performing what these functions originally do, save information about the calls somewhere, in a dynamically growing global array probably, protected by synchronization primitives for multithreaded programs.

此信息可能包括函数名称,请求的内存量,成功分配的块的地址,堆栈跟踪这让你知道呼叫者是什么,等等。在free()中,从数组中删除相应的元素(如果没有,将错误的指针传递给free,这也是早期被检测到的错误)。当程序结束时,转储数组的其余元素 - 它们将是泄露的块。不要忘记在main()之前和之后分配和释放资源的全局对象。为了正确计算这些资源,您需要在最后一个全局对象被销毁之后转储剩余的资源,因此您的编译器运行时的一小部分可能是必需的。

This information may include function name, amount of memory requested, address of the successfully allocated block, stack trace that lets you figure out what the caller was, and so on. In free(), remove corresponding element from the array (if there are none, a wrong pointer is passed to free which is also a error that's good to be detected early). When the program ends, dump the remaining elements of the array - they will be the blocks that leaked. Don't forget about global objects that allocate and deallocate resources before and after main(), respectively. To properly count those resources, you will need to dump the remaining resources after the last global object gets destroyed, so a small hack of your compiler runtime may be necessary

这篇关于内存泄漏调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆