Solved

memory debug in mfc

Posted on 2000-05-09
6
633 Views
Last Modified: 2013-11-20
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(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)
      | _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?
0
Comment
Question by:podb
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2792309
There are actually two things that can be done to prevent this:

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



0
 

Author Comment

by:podb
ID: 2792381
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(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)
      | _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.
0
 
LVL 3

Expert Comment

by:MDarling
ID: 2792634
There's a flag called afxMemDF which you can use.  Not sure you can only check at exit though.

regards,
Mike.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:podb
ID: 2792646
No, you cannot.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 2792753
Well, after swiching off the debug logging (as I erroneously suggested before), you could always call

_CrtDumpMemoryLeaks ();

before your program exits.

OR:

(at startup)

_CrtMemState state ;

_CrtMemCheckpoint( &state );

(at shutdown)

_CrtMemDumpAllObjectsSince( &state );
0
 

Author Comment

by:podb
ID: 2792857
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_STATE>::~CProcessLocal<_AFX_DEBUG_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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 163
Generic progress indicator 6 113
noX challenge 17 92
Problem to open Excel file 15 99
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now