Click here to Skip to main content
16,006,475 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to detect memory leak in VC++ Pin
yellowine19-Feb-02 5:54
yellowine19-Feb-02 5:54 
AnswerRe: How to detect memory leak in VC++ Pin
Joaquín M López Muñoz19-Feb-02 6:12
Joaquín M López Muñoz19-Feb-02 6:12 
AnswerRe: How to detect memory leak in VC++ Pin
Bill Wilson19-Feb-02 6:14
Bill Wilson19-Feb-02 6:14 
GeneralRe: How to detect memory leak in VC++ Pin
Nish Nishant19-Feb-02 18:47
sitebuilderNish Nishant19-Feb-02 18:47 
GeneralRe: How to detect memory leak in VC++ Pin
Shog919-Feb-02 19:45
sitebuilderShog919-Feb-02 19:45 
AnswerRe: How to detect memory leak in VC++ Pin
CreekDawg19-Feb-02 6:10
CreekDawg19-Feb-02 6:10 
AnswerRe: How to detect memory leak in VC++ Pin
Atul Dharne19-Feb-02 6:17
Atul Dharne19-Feb-02 6:17 
AnswerRe: How to detect memory leak in VC++ Pin
Paul M Watt19-Feb-02 13:24
mentorPaul M Watt19-Feb-02 13:24 
first make this declaration at the top of your stdafx.h file:

#define _CRTDBG_MAP_ALLOC

next make sure that these files are included in your project:

#include <stdlib.h>
#include <crtdbg.h>

finally, when your program shutsdown, you need to call this function:

_CrtDumpMemoryLeaks();

This will produce an output like this:

<br />
Detected memory leaks!<br />
Dumping objects -><br />
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18} normal block at 0x00780E80, 64 bytes long.<br />
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD <br />
Object dump complete.<br />


You can click on filename with the line number and it will take you to the place where you allocated the memory. This will be a good place to start.

After you get a list of objects that are leaking, you will now have the allocation number of the block that was leaked. In the example above (18) was the block.

You can run the program again and set a break point when block 18 is allocated. First set a break point right when your program loads.

Declare a variable in your watch window called:

_crtBreakAlloc

Set this variable to 18, this will tell the debugger to break when the 18 block is allocated. With this you can step through the code and know exactly which element that you allocated that is leaking. With this technique it is very important that you perform the exact same operations in the program in order to make the memory allocation follow the same path.

Look up this article on MSDN for more information about this technique:

Detecting and Isolating Memory Leaks Using Microsoft Visual C++ by Edward Wright

This technique is a good, free, place to start, but like other people have said, BoundsChecker is a good place to go if you have the money to buy this tool.
GeneralRe: How to detect memory leak in VC++ Pin
Rick York19-Feb-02 14:27
mveRick York19-Feb-02 14:27 
QuestionPartial Template Specialization? Pin
Jamie Hale19-Feb-02 5:45
Jamie Hale19-Feb-02 5:45 
AnswerRe: Partial Template Specialization? Pin
Nemanja Trifunovic19-Feb-02 5:57
Nemanja Trifunovic19-Feb-02 5:57 
GeneralRe: Partial Template Specialization? Pin
Jamie Hale19-Feb-02 6:03
Jamie Hale19-Feb-02 6:03 
GeneralRe: Partial Template Specialization? Pin
Joaquín M López Muñoz19-Feb-02 6:08
Joaquín M López Muñoz19-Feb-02 6:08 
GeneralRe: Partial Template Specialization? Pin
Nemanja Trifunovic19-Feb-02 6:10
Nemanja Trifunovic19-Feb-02 6:10 
GeneralRe: Partial Template Specialization? Pin
Tim Smith19-Feb-02 9:51
Tim Smith19-Feb-02 9:51 
GeneralRe: Partial Template Specialization? Pin
Christian Graus19-Feb-02 11:58
protectorChristian Graus19-Feb-02 11:58 
GeneralRe: Partial Template Specialization? Pin
Joaquín M López Muñoz19-Feb-02 19:49
Joaquín M López Muñoz19-Feb-02 19:49 
AnswerRe: Partial Template Specialization? Pin
Michael Dunn19-Feb-02 8:29
sitebuilderMichael Dunn19-Feb-02 8:29 
AnswerRe: Partial Template Specialization? Pin
Steen Krogsgaard20-Feb-02 0:03
Steen Krogsgaard20-Feb-02 0:03 
GeneralRe: Partial Template Specialization? Pin
Jamie Hale20-Feb-02 6:36
Jamie Hale20-Feb-02 6:36 
GeneralProblem with Class View VC++ 6 Pin
Mike Gaskey19-Feb-02 5:37
Mike Gaskey19-Feb-02 5:37 
GeneralRe: Problem with Class View VC++ 6 Pin
Rage19-Feb-02 5:43
professionalRage19-Feb-02 5:43 
GeneralRe: Problem with Class View VC++ 6 Pin
Jamie Hale19-Feb-02 6:25
Jamie Hale19-Feb-02 6:25 
GeneralRe: Problem with Class View VC++ 6 Pin
J Patel19-Feb-02 7:02
J Patel19-Feb-02 7:02 
GeneralAccessing Dialog Data in MFC Pin
Rage19-Feb-02 5:32
professionalRage19-Feb-02 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.