How do you detect/avoid Memory leaks in your (Unmanaged) code?(您如何检测/避免(非托管)代码中的内存泄漏?)
问题描述
在非托管 C/C++ 代码中,检测内存泄漏的最佳做法是什么?以及要避免的编码指南?(好像就这么简单;)
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++ 代码可移植到 *nix,那么没有什么比 Valgrind.
If your C/C++ code is portable to *nix, few things are better than Valgrind.
这篇关于您如何检测/避免(非托管)代码中的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:您如何检测/避免(非托管)代码中的内存泄漏?


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01