Sign in to follow this  
BEHOLDER192875

Debugging memory leaks in C++

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[quote name='BEHOLDER192875' timestamp='1307113404' post='4819074']
#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
[/quote]

Do you have this included very first thing in every compilation unit? Do you have any singletons or any other static objects?

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
[quote name='BEHOLDER192875' timestamp='1307117542' post='4819115']
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();
[/quote]
You could also use this [url="http://www.codeproject.com/KB/applications/visualleakdetector.aspx"]VLD[/url] 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this