Adelost
Adelost

Reputation: 2453

Possible to hide safe memory leak from leak detection in visual studio?

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. ;)

Upvotes: 3

Views: 796

Answers (1)

Alex F
Alex F

Reputation: 43331

_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

Upvotes: 4

Related Questions