?
Solved

familiar with _CrtMemDumpStatistics()?

Posted on 2003-02-19
6
Medium Priority
?
1,908 Views
Last Modified: 2007-12-19
I set CrtMemCheckpoint before and after my allocation using two different _CrtMemState.  With a third _CrtMemState i am reporting the memory allocations with _CrtMemDumpStatistics.  I get plenty of feedback in my debug window but dont understand exactly what the feedback means.  Maybe someone can help me out.
I think my confusion is what the output really means.  Here are the first few calls:

0 bytes in 0 Free Blocks.
81 bytes in 2 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
72 bytes in 1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 384 bytes.
0 bytes in 0 Free Blocks.
105 bytes in 4 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
120 bytes in 2 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 687 bytes.
0 bytes in 0 Free Blocks.
334 bytes in 2 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
72 bytes in 1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 629 bytes.
0 bytes in 0 Free Blocks.
97 bytes in 2 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
72 bytes in 1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 401 bytes.

And if you can answer that then maybe you can shead light on this:

0 bytes in 0 Free Blocks.
77 bytes in 2 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
72 bytes in 1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 384 bytes.
0 bytes in 0 Free Blocks.
-215 bytes in -4 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
-80 bytes in -1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 123 bytes.
0 bytes in 0 Free Blocks.
-215 bytes in -4 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
-80 bytes in -1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 123 bytes.
0 bytes in 0 Free Blocks.
95 bytes in 2 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
72 bytes in 1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 402 bytes.


Whats with negative bytes in negative blocks?


Thanks in advance
0
Comment
Question by:Smugindividual
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:corduroy9
ID: 7983941


I've worked with this a bit.  I used CMemoryState objects to test for memory leaks and such.  

The output is telling you how much memory was allocated since the last checkpoint.  I found that using these objects has been pretty difficult.  It forces you to break up your code into small units of work, and if allocation and objects are used throughout a large process, it will definitely show that you have been allocating resources, depending on where you put your checkpoints, so it's somewhat deceiving it that respect.

Look up CMemoryState, here's a good starting point...  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cmemorystate.asp
...for more information about debugging memory leaks.

Good luck
0
 

Author Comment

by:Smugindividual
ID: 7988006
The leak is definetly occuring in one function.  I put the checkpoints at the beginning and end of the function.  That was the feedback i got, what i was wondering is does this:

0 bytes in 0 Free Blocks.
81 bytes in 2 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
72 bytes in 1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 384 bytes.

mean that 81 bytes and 72 bytes were allocated and never freed?  And does this:

0 bytes in 0 Free Blocks.
-215 bytes in -4 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
-80 bytes in -1 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 123 bytes.

mean that more memory was freed than allocated?  And if so, how can i have a positive total allocation when the only listed allocations were negative?
0
 
LVL 2

Expert Comment

by:corduroy9
ID: 7993064


I found this article in MSDN (on my machine, but I couldn't find it online).  It is in my next comment and explains the negative numbers better:

good luck!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
corduroy9 earned 200 total points
ID: 7993067
INFO: Foundation Class Debug Library Detects Memory Leaks
ID: Q99022


--------------------------------------------------------------------------------
The information in this article applies to:

The Microsoft Foundation Classes (MFC), used with:
Microsoft C/C++ for MS-DOS, version 7.0
Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51, 1.52
Microsoft Visual C++, 32-bit Editions, version 1.0

--------------------------------------------------------------------------------


SUMMARY
Version 2.0 of the Microsoft Foundation Classes debugging libraries provide automatic memory leak detection, a new feature that version 1.0 did not provide.

In an application developed with a Microsoft Foundation Classes version 1.0 library, the application object code typically calls the Checkpoint() function in CMemoryState objects in its InitInstance() and ExitInstance() functions. Then the application calls the CMemoryState::Difference() function to verify that no memory leaked during execution. This process is not necessary when the application uses library version 2.0.

The Microsoft Foundation Classes version 2.0 debugging libraries automatically perform memory leak detection. The detection code is in AFXMEM.CPP, which is installed in the C:\MSVC\MFC\SRC directory by default. This code detects the case in which an application dynamically allocates an object and fails to delete the object before the program terminates. The library sends a TRACE() message to the debug window stating that memory has leaked. To see these messages, TRACE() output must be enabled. Please refer to MFC Tech Note #7 for more information about enabling TRACE() output.

In version 2.0, placing a CMemoryState::Checkpoint() call in InitInstance() and ExitInstance(), as demonstrated in the code example below, does not function correctly. The code example below produces the following output:


   Memory leaked
   0 bytes in 0 Free Blocks
   0 bytes in 0 Object Blocks
   -56 bytes in -4 Non-Object Blocks
The negative numbers indicate that memory is allocated before the Checkpoint() call in InitInstance() and freed before the Checkpoint() call in ExitInstance().

The output shown above is for an application built using the medium memory model. An application built using the large memory model will double the byte count shown in the last line of output like so:
-112 bytes in -4 Non-Object blocks
In the Microsoft Foundation Classes Library source code file WINHAND.CPP, line 75 calls InitHashTable(). The code is as follows:

   m_temporaryMap.InitHashTable(7); // small table for temporary map
The code calls this function for each CHandleMap object: _afxMapHDC, _afxMapHGDIOBJ, _afxMapHMENU, and _afxMapHWND. These hash tables contain the permanent and temporary maps for window handles and their associated C++ objects.

The initial temporary maps that this function creates are destroyed in the CWinApp::OnIdle() function. In order for manual memory leak detection to work correctly, these temporary maps may be removed before your first memory checkpoint is initialized by calling DeleteTempMap() for each of the CHandleMap objects. In the code example below, removing the comment indication from the specified lines of code deletes the temporary maps and enables the manual memory leak detection code to work correctly.

As mentioned above, the Microsoft Foundation Classes debugging libraries for version 2.0 automatically detect memory leaks and the leak detection code shown below is not necessary.
More Information
The problem described in this article does not occur in the 32-bit versions of MFC included with Microsoft Visual C++ 2.0 or later.
Sample Code

/*
 * Compile options needed: None
 */

static CMemoryState oldstate, newstate, diffstate;

BOOL CTheApp::InitInstance()
{
   // Remove the comment indication from the following 4 lines to
   // delete the temporary maps and allow the code to detect true
   // memory leaks.
   // CGdiObject::DeleteTempMap();
   // CDC::DeleteTempMap();
   // CMenu::DeleteTempMap();
   // CWnd::DeleteTempMap();

   oldstate.Checkpoint();

   m_pMainWnd = new CMainWindow();
   m_pMainWnd->ShowWindow( m_nCmdShow );
   m_pMainWnd->UpdateWindow();

   return TRUE;
}

int CTheApp::ExitInstance()
{
   newstate.Checkpoint();
   if (diffstate.Difference(oldstate, newstate))
      {
      TRACE("Memory leaked\n");
      diffstate.DumpStatistics();
      }
   return 0;
}

Additional query words: kbinf 1.00 1.50 1.51 1.52 2.00 2.50 2.51 2.52

Keywords : kbDebug kbMFC kbVC100 kbVC150 kbGrpMFCATL
Version : winnt:1.0
Platform : winnt
Issue type : kbinfo
Technology : kbvc


Last Reviewed: February 4, 2000
) 2000 Microsoft Corporation. All rights reserved. Terms of Use.
 



--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
0
 
LVL 2

Expert Comment

by:corduroy9
ID: 7993078


...and this (unrelater) article shows the negative numbers as a definitely memory leak (in the example at the bottom) but doesn't really explain much more.  Hope this helps...


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cmemorystate.3a3a.dumpstatistics.asp

0
 

Author Comment

by:Smugindividual
ID: 7999025
Thanks for the help
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 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