Sign in to follow this  

Memory Management Library

This topic is 3298 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

Anyone know of a free C++ memory management library ( you know, overwrites new and delete and keeps track of allocation information such as file name and line number and allows me to print out memory reports and find leaks ) that will work with STL?

Share this post


Link to post
Share on other sites
Quote:
Original post by RegularKid
Anyone know of a free C++ memory management library ( you know, overwrites new and delete and keeps track of allocation information such as file name and line number and allows me to print out memory reports and find leaks ) that will work with STL?


Most environments provide libraries or code to do this. But the truth is, you'll only really need them if you do something 'wrong'.

IMVHO, associating each resource with the lifetime of an object (using RAII, the rule of three and various smart pointers) will get you 99.9% of the way there. The only reason to move away from these constructs is in the case that performance really, really matters and you've discovered via profiling that you can squeeze out an extra 0.5% by doing things manually in a couple of places.

Writing programs with constructs guaranteed not to leak resources gives me a nice warm, fuzzy feeling. I've never felt the need to look for a leak-checking tool, unless it was for someone else's code ;)

Putting my soap box aside, there's a pretty good program called "leaks" on Mac OS X and for MS Visual C++, using the "debug heap" might get you what you want.

Share this post


Link to post
Share on other sites
Hmmm, the debug heap looks like what I want to use...although I can't seem to get it to work. Here's a test program I created:


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

int main()
{
{
char* pTest = new char[ 50 ];
}

_CrtDumpMemoryLeaks();

return 0;
}



And here's the output returned:


Detected memory leaks!
Dumping objects ->
{77} normal block at 0x00366DF8, 50 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.



All the searching I did on the debug heap and crtdbg say that you'll get memory allocation information ( filename / line number ) for leaks by using #define _CRTDBG_MAP_ALLOC. Am i doing something wrong here?

Share this post


Link to post
Share on other sites
Default operator new does not use the debug malloc and free, they use the normal one. Override it and you can get the output to work, but it points to the location that malloc was called, in operator new so it's kinda useless.


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

void *operator new (size_t size)
{
return malloc(size);
}

void *operator new [](size_t size)
{
return malloc(size);
}

void operator delete (void *ptr)
{
if (ptr == NULL)
return;

return free(ptr);
}

void operator delete [](void *ptr)
{
if (ptr == NULL)
return;

return free(ptr);
}

int main(int argc, char* argv[])
{
{
char* pTest = new char[ 50 ];
}

_CrtDumpMemoryLeaks();

return 0;
}



I usually write my own memory library for debugging. Taking a runtime call stack at every allocation can be very helpful, but use of RAII has mostly removed any need for memory leak debugging.

Although just having the CrtDumpMemoryLeaks from the beginning can be helpful as well. Notice when the output tells you that you have leaked memory and you will know that the culprit is likly in your newest code.

Share this post


Link to post
Share on other sites

This topic is 3298 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.

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