4

I recently converted a program using raw pointers to std::shared_ptrs and wanted to ensure there was no memory leak. I looked at MSDN's Finding Memory Leaks Using the CRT Library page and followed the instructions to set up the report.

I begin by adding these lines to my file:

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

and call the report with - _CrtDumpMemoryLeaks();

I am indeed using new to allocate my memory so, as the page suggests, I added...

#ifdef _DEBUG   
#ifndef DBG_NEW      
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )      
#define new DBG_NEW   
#endif
#endif  // _DEBUG

with no significant change in terms of a more precise location of the leak. Instead my report still looks like this:

Detected memory leaks!
Dumping objects ->
{158} normal block at 0x006F8458, 8 bytes long.
 Data: <  ,     > DC F6 2C 00 00 00 00 00 
{155} normal block at 0x006FAD40, 40 bytes long.
 Data: <L>              > 4C 3E 17 01 03 00 00 00 01 00 00 00 CD CD CD CD 
{154} normal block at 0x006FAB68, 16 bytes long.
 Data: <d>              > 64 3E 17 01 01 00 00 00 01 00 00 00 00 00 00 00 
{149} normal block at 0x006FAC08, 40 bytes long.
 Data: <L>              > 4C 3E 17 01 03 00 00 00 01 00 00 00 CD CD CD CD 
{148} normal block at 0x006FABB8, 16 bytes long.
 Data: <d>              > 64 3E 17 01 01 00 00 00 01 00 00 00 00 00 00 00 
Object dump complete.
The program '[7152] List.exe' has exited with code 0 (0x0).

Unfortunately, the report is generated just prior to exit so it's not possible to see the leak in real time.

By looking at an answer to this question I noticed that I was not loading the pdb file due to the fact it's an empty project. So I followed the advice given here which did fix the pdb problem but failed to fix my issue.

The app was generated from an Empty Console Application, if that matters, and I'm running as an administrator.

Also I'm aware of external tools I could use but I often use computers that I don't have permission to add any to so getting VS to function properly is the ideal solution. Any help getting the report to show the line numbers of the leaks would be great.

Side Note:

I suppose a logical question to ask would be - If you're using managed pointers why do I still have memory leak? Well, I left a couple localized pointers that are only temp holders in sorts etc. that seemingly should not have caused an issue. So, I feel like I know where it is occurring but this is such a simple program I would like to know how this is accomplished so I can use it on larger ones. Thanks.

Community
  • 1
  • 1
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • 1
    Have you read "Interpreting the Memory-Leak Report" section of the documentation that you refer to? It seems like you haven't defined `_CRTDBG_MAP_ALLOC` that makes the detection show the file and line where the resource was first allocated. –  Mar 04 '13 at 02:06
  • 1
    don't know about the MSVC stuff, but you might be leaking due to cyclic references – Stephen Lin Mar 04 '13 at 02:06

3 Answers3

3

You need to make sure that the defines and the inclusion of stdlib are before any other includes in the application.

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif  // _DEBUG

Should be the very first thing in your application.

Matt Lord
  • 49
  • 1
  • 6
  • I think that's the way it was. I had it the same as the link in the post. Unfortunately, I don't have access to that code any longer. If/when I need this again I'll report back... – ChiefTwoPencils Oct 02 '15 at 18:16
  • 1
    No problem, I came across this post after I had encountered the same problem, I posted here for other users to stumble across! – Matt Lord Oct 06 '15 at 08:20
0

You have to verify that all classes that you declare inside your project include crtdbg.h and the macro _crtdbg_map_alloc and the report at the end.

pablo285
  • 2,460
  • 4
  • 14
  • 38
0

I found that I had to put the

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

in every file that might be the potential source of the allocation. I made the assumption that I only needed to do it in the main program where the

_CrtDumpMemoryLeaks();

is called.

Syscall
  • 19,327
  • 10
  • 37
  • 52
Brian Reinhold
  • 2,313
  • 3
  • 27
  • 46