Reputation: 13940
I recently converted a program using raw pointers to std::shared_ptr
s 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.
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.
Upvotes: 4
Views: 3121
Reputation: 2475
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 49
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.
Upvotes: 3