podb
asked on
memory debug in mfc
Is there a way to alter the memory debug behavior of mfc?
Mfc checks for memory leaks *before* the program exits (thus reporting
memory leaks on globals). This generates a lot of messages in my
application (and I get annoyed by them). (for instance, try this:
#include <iostream> #include <crtdbg.h> void main () {
_CrtDumpMemoryLeaks() }; ,this'll generate false memory leak messages
and this is exactly what mfc does).
Normally you can do something like :
_CrtSetDbgFlag(_CrtSetDbgF lag(_CRTDB G_REPORT_F LAG)
| _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF
| _CRTDBG_CHECK_ALWAYS_DF);
,but I guess mfc resets those flag bits.
So, can anyone advise me on this one?
Mfc checks for memory leaks *before* the program exits (thus reporting
memory leaks on globals). This generates a lot of messages in my
application (and I get annoyed by them). (for instance, try this:
#include <iostream> #include <crtdbg.h> void main () {
_CrtDumpMemoryLeaks() }; ,this'll generate false memory leak messages
and this is exactly what mfc does).
Normally you can do something like :
_CrtSetDbgFlag(_CrtSetDbgF
| _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF
| _CRTDBG_CHECK_ALWAYS_DF);
,but I guess mfc resets those flag bits.
So, can anyone advise me on this one?
ASKER
I don't want to prevent memory debug, I only want to do it at program exit. Normally you do that by setting the _CRTDBG_ALLOC_MEM_DF flag.
I have tried to add this (see *) to my application constructor (is this the correct place to add this?), but it didn't help (that's why I thought mfc is calling _CrtSetDbgFlag itself).
(*)
_CrtSetDbgFlag(_CrtSetDbgF lag(_CRTDB G_REPORT_F LAG)
| _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF
| _CRTDBG_CHECK_ALWAYS_DF);
So, the question is: how do I tell mfc to only do a memory leak test at program exit?
By the way, jkr, thanks for taking the time.
I have tried to add this (see *) to my application constructor (is this the correct place to add this?), but it didn't help (that's why I thought mfc is calling _CrtSetDbgFlag itself).
(*)
_CrtSetDbgFlag(_CrtSetDbgF
| _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF
| _CRTDBG_CHECK_ALWAYS_DF);
So, the question is: how do I tell mfc to only do a memory leak test at program exit?
By the way, jkr, thanks for taking the time.
There's a flag called afxMemDF which you can use. Not sure you can only check at exit though.
regards,
Mike.
regards,
Mike.
ASKER
No, you cannot.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I see no good reason to call _CrtDumpMemoryLeaks(). This will always be called *before* globals, etc. are destroyed. Setting the flag _CRTDBG_ALLOC_MEM_DF, however gives the right behavior.
I have done some tracking myself and in ...\mfc\src\dumpinit.cpp, line 131, in the destructor of _AFX_DEBUG_STATE you'll see the call to _CrtDumpMemoryLeaks(), which is called through CProcessLocal<_AFX_DEBUG_S TATE>::~CP rocessLoca l<_AFX_DEB UG_STATE>( ) (This is called when there are still 8 calls on the call stack).
But hey, you can have the 200 points. I'm starting to belief there is no good solution.
I have done some tracking myself and in ...\mfc\src\dumpinit.cpp, line 131, in the destructor of _AFX_DEBUG_STATE you'll see the call to _CrtDumpMemoryLeaks(), which is called through CProcessLocal<_AFX_DEBUG_S
But hey, you can have the 200 points. I'm starting to belief there is no good solution.
- turn off the debugging flags
// Get the current state of the flag
// and store it in a temporary variable
int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
tmpFlag &= ~_CRTDBG_CHECK_ALWAYS_DF;
tmpFlag &= ~_CRTDBG_LEAK_CHECK_DF ;
tmpFlag &= ~_CRTDBG_ALLOC_MEM_DF ;
// Set the new state for the flag
_CrtSetDbgFlag( tmpFlag );
- 'swallow' the messages:
int _CRTAPI1 __MygReportHook ( int nType,
char *pszMsg,
int *pnRetVal
)
{
return( TRUE);
}
_CrtSetReportHook ( ( _CRT_REPORT_HOOK) __MyReportHook);