3

Is there any way to hide a safe memory leak from normal memory detection in visual studio?

I am detecting memory leaks using this debug flag:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

I just spend a lot of time on finding out how to embedd Boost.Python in C++ project. It worked great, and I was reallty impressed by the flexibility I would be having. All hard work had finally paid off. That is, until I came to the soul crushing realisation, the Python integration has a memory leak. It appears to be a know problem and won't be fixed Does the Python 3 interpreter leak memory when embedded?. Since the memory leak remains constant, they say it can be safely ignored. However, using leak detection in Visual Studio is a huge help for me, and having a false positives show up everytime I run the program will make it a lot harder to detect real memory leaks. I don't wanna give that up, but I don't wanna give up python eather.

Is there any way for me to hide the memory leaks? Wrapping the code in a static library, DLL, separate process, anything?! If I'm sounding desperate it's because that's pretty close to what I feel. ;)

Community
  • 1
  • 1
Adelost
  • 2,343
  • 2
  • 23
  • 28
  • Do you want to hide memory leaks from specific code fragments, or leaks created by some third-party code placed in another library? – Alex F May 22 '14 at 04:49

1 Answers1

4
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & ~_CRTDBG_ALLOC_MEM_DF);

// allocations here are ignored by memory leaks tracker

_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_ALLOC_MEM_DF);

// memory leaks tracking continues

Generic version which restores memory leaks tracking to its original state:

int flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
_CrtSetDbgFlag(flags & ~_CRTDBG_ALLOC_MEM_DF);

// allocations here are ignored by memory leaks tracker

_CrtSetDbgFlag(flags);

// memory leaks tracking returns to its original state
Alex F
  • 42,307
  • 41
  • 144
  • 212
  • It should also be an '_' in front of the second `_CRTDBG_ALLOC_MEM_DF`. It's a very obvious mistake, but perhaps you should fix it for completeness. – Adelost May 22 '14 at 06:19
  • Fixed, it was cut-and-paste mistake. Thanks. – Alex F May 22 '14 at 06:20