Debugging memory leaks in C++

Started by
4 comments, last by BEHOLDER192875 12 years, 10 months ago
After adding these lines:

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

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
.
.
.
WinMain()
{
.
.
.
_ASSERTE(_CrtCheckMemory());
_CrtDumpMemoryLeaks();

return somevalue;
}

you would think that it would report all memory leak locations; however, it does not. I am still detecting memory leaks, so I thought it could be a false positive, but I just wanted to be sure that this was the correct way to check for memory leaks in a Windows application.
Advertisement

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

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif


Do you have this included very first thing in every compilation unit? Do you have any singletons or any other static objects?
I only included that in the main.cpp file as everything else is in a header. I do have several static objects, but I am calling their Release() function.

The only variables that should be residing in memory are not going to be using new or delete or any memory allocation, i.e. int, char, etc.

I thought that it could be because of my use of vector to store images, and maybe I am not releasing them correctly.

It would be something like:

vector<LPDIRECT3DTEXTURE9> image;

for(UINT x=0; x<image.size(); x++)
image[x]->Release();

image.clear();

I only included that in the main.cpp file as everything else is in a header. I do have several static objects, but I am calling their Release() function.

The only variables that should be residing in memory are not going to be using new or delete or any memory allocation, i.e. int, char, etc.

I thought that it could be because of my use of vector to store images, and maybe I am not releasing them correctly.

It would be something like:

vector<LPDIRECT3DTEXTURE9> image;

for(UINT x=0; x<image.size(); x++)
image[x]->Release();

image.clear();

You could also use this VLD which does the same thing but is already written for you. It will also give you full callstacks of where the leaks where generated which are very handy I can tell you from experience. Bear in mind that VLD uses the standard MS API to figure out where memory leaks are, so if you are using your own memory management (eg use of placement new operator) this tool won't be very useful to you.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

What you have looks fine to me...it will generally dump out any leaks to the console on exit. If the debug_new is not defined then it will still dump out the leak, but if you double click it in the console then it will not take you too the line that created it.

I don't think it will dump out leaks from things like COM (directx). I don't remember ever seeing that in practice, but pretty much all the others showed up fine.

You might try adding:
char* temp = new char[20];
and see if it reports that leak just to make sure it is indeed dumping as expected
Thanks NightCreature83. I had no idea that VLD would be so easy to install and use. I detected no memory leaks at all, so that goes to show you that the built-in memory leak detector in MSVC++ is highly flawed, and VLD confirmed that I was detecting false positives.

This topic is closed to new replies.

Advertisement