如何忽略来自 _CrtDumpMemoryLeaks 的误报内存泄漏?

How to ignore false positive memory leaks from _CrtDumpMemoryLeaks?(如何忽略来自 _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 的误报内存泄漏?

基础教程推荐