How to ignore false positive memory leaks from _CrtDumpMemoryLeaks?(如何忽略来自 _CrtDumpMemoryLeaks 的误报内存泄漏?)
问题描述
似乎只要有静态对象,_CrtDumpMemoryLeaks 就会返回一个误报,声称它正在泄漏内存.我知道这是因为它们直到 main()(或 WinMain)函数之后才会被销毁.但是有没有办法避免这种情况?我用的是VS2008.
It seems whenever there are static objects, _CrtDumpMemoryLeaks returns a false positive claiming it is leaking memory. I know this is because they do not get destroyed until after the main() (or WinMain) function. But is there any way of avoiding this? I use VS2008.
推荐答案
我发现如果你告诉它在程序终止后自动检查内存,它允许所有静态对象都被考虑在内.我正在使用 log4cxx 和 boost 在静态块中进行大量分配,这修复了我的误报"...
I found that if you tell it to check memory automatically after the program terminates, it allows all the static objects to be accounted for. I was using log4cxx and boost which do a lot of allocations in static blocks, this fixed my "false positives"...
在 main() 开头的某处添加以下行,而不是调用 _CrtDumpMemoryLeaks:
Add the following line, instead of invoking _CrtDumpMemoryLeaks, somewhere in the beginning of main():
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
有关用法和宏的更多详细信息,请参阅 MSDN 文章:
For more details on usage and macros, refer to MSDN article:
http://msdn.microsoft.com/en-us/library/5at7yxcs(v=vs.71).aspx
这篇关于如何忽略来自 _CrtDumpMemoryLeaks 的误报内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何忽略来自 _CrtDumpMemoryLeaks 的误报内存泄漏?


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