How to ignore false positive memory leaks from _Cr

2019-01-12 00:11发布

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.

5条回答
Summer. ? 凉城
2楼-- · 2019-01-12 00:48

1) You said:

It seems whenever there are static objects, _CrtDumpMemoryLeaks returns a false positive claiming it is leaking memory.

I don't think this is correct. EDIT: Static objects are not created on heap. END EDIT: _CrtDumpMemoryLeaks only covers crt heap memory. Therefore these objects are not supposed to return false positives. However, it is another thing if static variables are objects which themselves hold some heap memory (if for example they dynamically create member objects with operator new()).

2) Consider using _CRTDBG_LEAK_CHECK_DF in order to activate memory leak check at the end of program execution (this is described here: http://msdn.microsoft.com/en-us/library/d41t22sb(VS.80).aspx). I suppose then memory leak check is done even after termination of static variables.

查看更多
欢心
3楼-- · 2019-01-12 00:54

Not a direct solution, but in general I've found it worthwhile to move as much allocation as possible out of static initialization time. It generally leads to headaches (initialization order, de-initialization order etc).

If that proves too difficult you can call _CrtMemCheckpoint (http://msdn.microsoft.com/en-us/library/h3z85t43%28VS.80%29.aspx) at the start of main(), and _CrtMemDumpAllObjectsSince at the end.

查看更多
叛逆
4楼-- · 2019-01-12 00:57

Can you take a snapshot of the currently allocated objects every time you want a list? If so, you could remove the initially allocated objects from the list when you are looking for leaks that occur in operation. In the past, I have used this to find incremental leaks.

Another solution might be to sort the leaks and only consider duplicates for the same line of code. This should rule out static variable leaks.

Jacob

查看更多
Rolldiameter
5楼-- · 2019-01-12 00:59

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"...

Add the following line, instead of invoking _CrtDumpMemoryLeaks, somewhere in the beginning of main():

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

For more details on usage and macros, refer to MSDN article:

http://msdn.microsoft.com/en-us/library/5at7yxcs(v=vs.71).aspx

查看更多
来,给爷笑一个
6楼-- · 2019-01-12 01:08

Ach. If you are sure that _CrtDumpMemoryLeaks() is lying, then you are probably correct. Most alleged memory leaks that I see are down to incorect calls to _CrtDumpMemoryLeaks(). I agree entirely with the following; _CrtDumpMemoryLeaks() dumps all open handles. But your program probably already has open handles, so be sure to call _CrtDumpMemoryLeaks() only when all handles have been released. See http://www.scottleckie.com/2010/08/_crtdumpmemoryleaks-and-related-fun/ for more info.

查看更多
登录 后发表回答