Jump to content
  • Advertisement
Sign in to follow this  
BEHOLDER192875

Debugging memory leaks in C++

This topic is 2578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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?

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

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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!