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

2021-12-21 00:00:00 memory static memory-leaks c++

似乎只要有静态对象,_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

相关文章