Turning off _CrtDumpMemoryLeaks()

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
We have a fairly large old native application (6 million lines of code, 200+ DLLs), and I am having a problem with <span style="color:red <b><span style="color:red <b>_CrtDumpMemoryLeaks</b></b>() getting called when the application exits. I actually want to *prevent* <span style="color:red <b><span style="color:red <b>_CrtDumpMemoryLeaks</b></b>()  from dumping memory leaks to the debug output window.
 
What happens in our application is that we explicitely unload all the DLLs that we explicitely loaded. We do this by calling FreeLibrary(). Fine that has worked for years now. The problem is that one of our DLLs depends (I believe) on an mfc DLL. Therefore when our DLL is unloaded, so does the mfc DLL. (I believe its mfc80.dll... Im going by memory here since Im at home). And when the mfc DLL gets unloaded, it calls the above mentioned function, and out comes the large long list of memory leaks (At least 10,000+ lines of it... ). The memory leaks are useless since no file location is specified. (BTW: We use boundschecker to catch memory leaks... for now)
 
We didnt have this problem until just relatively recently, then all of a sudden it started happening last Fall.
 
I was able to put a breakpoint in <span style="color:red <b><span style="color:red <b>_CrtDumpMemoryLeaks</b></b>()  to verify who is calling this function, and I did find that it was an mfc DLL. I suppose the dll is unloaded since (most likely) our application DLL has a dependency on it.
 
<u>Attempted Resolution:</u>
 
I have attempted to turn off the flag for memory leak checks right before we call our FreeLibrary routines:
 <font color="#008000" size=2>
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet <font color="#008000" size=2>
// Get current flag </font><font color="#0000ff" size=2>
int</font><font size=2> tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); </font><font color="#008000" size=2>
// Turn off leak-checking bit. </font><font size=2>
tmpFlag &= ~_CRTDBG_LEAK_CHECK_DF; </font><font color="#008000" size=2>
// Turn off CRT block checking bit. </font><font size=2>
tmpFlag &= ~_CRTDBG_CHECK_CRT_DF; </font><font color="#008000" size=2>
// Set flag to the new value. </font><font size=2>
_CrtSetDbgFlag( tmpFlag ); </font>
 </font>
However it didnt work.
 
I put a breakpoint in _CrtSetDbgFlag and it gets called nearly 110 times afterwards. Yikes!
 
I have put this code snippet in lots of different places, all to no avail. The mfc dll calls the memory leak dumper, and there doesnt seem to be anything I can do about it.  <img height=19 alt=Sad src="http://forums.microsoft.com/MSDN/emoticons/emotion-6.gif" width=19>
 
Do I need to research this more on my end before asking for more help here? Or am I on the right track?
 
Any help would be appreciated. And I will check this thread throughout the course of the day.
 
Chris Johnson

View the full article
 
Back
Top