| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| CodeGuru Individual FAQs The indivdual FAQs for CodeGuru. See the specific Topic FAQ forums for index pages and links to these Frequently Asked/Answered Questions. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Visual C++ Debugging: How to manage memory leaks?
Q: What is a memory leak?
A: The failure to properly deallocate memory that was previously allocated. Q: What are the consequences of memory leaks? A: Programs that leak large amounts of memory, or leak progressively, may display symptoms ranging from poor (and gradually decreasing) performance to running out of memory completely. Worse, a leaking program may use up so much memory that it causes another program to fail, leaving the user with no clue to where the problem truly lies. In addition, even harmless memory leaks may be symptomatic of other problems. Q: How can a memory leak appear? A: There are several causes:
Q: How can I find if my program has memory leaks? A: When you run your program under the debugger, '_CrtDumpMemoryLeaks()' displays memory leak information in the output window. The memory leak information looks like this: Code:
Detected memory leaks!
Dumping objects ->
D:\VisualC++\CodeGuru\MemoryLeak\MemoryLeak.cpp(67) : {60}
normal block at 0x00324818, 4 bytes long.
Data: <, > 2C 00 00 00
Object dump complete.
Code:
Detected memory leaks!
Dumping objects ->
{60} normal block at 0x00324818, 4 bytes long.
Data: <, > 2C 00 00 00
Object dump complete.
Q: What is the effect of using '_CRTDBG_MAP_ALLOC' on the C++ 'new' and 'delete' operators? A: When the '_CRTDBG_MAP_ALLOC' flag is defined in the debug version of an application, the base version of the heap functions are directly mapped to their debug versions. This flag is only available when the '_DEBUG' flag has been defined in the application. The debug versions of the C run-time library contain debug versions of the C++ 'new' and 'delete' operators. If your C++ code defines 'CRTDBG_MAP_ALLOC', all instances of new are mapped to the debug version, which records source file and line number information. If you want to use the '_CLIENT_BLOCK' allocation type, do not define 'CRTDBG_MAP_ALLOC'. Instead, you must call the Debug version of the 'new' operator directly or create macros that replace the 'new' operator in debug mode, as shown in the following example. The debug version of the 'delete' operator works with all block types and requires no changes in your program when you compile a release version. Code:
// DbgNew.h
// Defines global operator new to allocate from client blocks
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
// MyApp.cpp
// Compile options needed: /Zi /D_DEBUG /MLd or use a
// Default Workspace for a Console Application to build a debug version
#include <crtdbg.h>
#include <dbgnew.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
int main( )
{
int* array = new int[10];
_CrtMemDumpAllObjectsSince(NULL);
return 0;
}
Q: Are there 'CRT' functions that report the state and content of the heap that can help me to detect memory leaks? A: In 'crtdbg.h' the following structure is defined: Code:
typedef struct _CrtMemState
{
struct _CrtMemBlockHeader* pBlockHeader;// Pointer to the most recently allocated block
unsigned long lCounts[_MAX_BLOCKS]; // A counter for each of the 5 types of block
unsigned long lSizes[_MAX_BLOCKS]; // Total bytes allocated in each block type
unsigned long lHighWaterCount; // The most bytes allocated at a time up to now
unsigned long lTotalCount; // The total bytes allocated at present
} _CrtMemState;
The following functions report the state and contents of the heap, and use the information to help detect memory leaks and other problems:
Q: How can I dump memory leak information? A: You can dump memory leak information by including the following statement in your program: Code:
#include <iostream>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
int main()
{
int* array = new int[10];
_CrtDumpMemoryLeaks();
return 0;
}
Q: How MFC helps me to detect memory leaks? A: You can make use of:
Q: Cay you give me an example? A: This example shows how to take snapshots of memory to help locate a memory leak. Notice that the memory-checking statements are bracketed by #ifdef _DEBUG / #endif blocks so that they are compiled only in Win32 Debug versions of your program. Code:
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
#endif
// Do your memory allocations and deallocations.
CSite* p = new CSite( "CodeGuru", "http://www.codeguru.com");
#ifdef _DEBUG
newMemState.Checkpoint();
if( diffMemState.Difference( oldMemState, newMemState ) )
{
TRACE( "Memory leaked!\n" );
}
#endif
Q: How can I avoid getting memory leaks? A: Make sure that:
Last edited by Andreas Masur; July 25th, 2005 at 02:15 AM. |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|