问题描述
在非托管的C / C ++ code,有什么办法检测内存泄漏的最佳实践?和编码准则,以避免? (好像就这么简单;)
In unmanaged C/C++ code, what are the best practices to detect memory leaks? And coding guidelines to avoid? (As if it's that simple ;)
我们已经使用了一个有点傻的方式过去:具有每个内存分配调用和递减计数器增量的同时释放。在节目的结尾,计数器值应该是零。
We have used a bit of a silly way in the past: having a counter increment for every memory allocation call and decrement while freeing. At the end of the program, the counter value should be zero.
我知道这是不是一个伟大的方式,有一些抓住。 (举例来说,如果你正在释放它是由一个平台的API调用分配的内存,您的分配数量将不完全匹配您的释放数量。当然,然后调用该分配的存储API调用时,我们增加了柜台。)
I know this is not a great way and there are a few catches. (For instance, if you are freeing memory which was allocated by a platform API call, your allocation count will not exactly match your freeing count. Of course, then we incremented the counter when calling API calls that allocated memory.)
我期待您的经验,建议也许有些引用到简化的工具。
I am expecting your experiences, suggestions and maybe some references to tools which simplify this.
干杯
推荐答案
如果您的C / C ++ code是移植到* nix中,几件事情比的。
If your C/C++ code is portable to *nix, few things are better than Valgrind.
这篇关于如何检测/避免你的(非托管)code内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!