memory leaks

Started by
3 comments, last by phil_t 17 years, 6 months ago
I'm writing a program in VC++ and noticed that as the program went on it got slower and slower, so i checked the task manager and sure enough the memory usage of my program kept on getting bigger and bigger. how do i find the source of this problem? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
You could try this article or this article, both from MSDN, the documentation on Visual Studio development.
enum Bool { True, False, FileNotFound };
Thanks, i tried to do what it said in the articles and it worked, in the output window it said that it detected a leak and it said were it was in the memory but it didn't say were it was in the code - even though i defined
#define CRTDBG_MAP_ALLOC
what could the problem be?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Memory leaks are usually because you don't use 'delete' on every 'new' operators, or malloc() and free() for older stuff. Just look into every part that does this.

Also, make sure you are freeing up the information before you try to allocate more.

for example:
int x, *pointer;for(x=0;x<10;x++){     pointer=new int[10];     /* do things with pointer */}delete pointer;


this makes it abundantly obvious, but with a real program I'm sure it'd be much easier to miss. :P

Sorry if I missed a reason... it's been a while and I'm rather against thinking right now...

edit: If I remember, there's at least one other thing in managed code that dynamically allocates memory, gcnew, that has to be manually released, so keep that in mind... again, sorry if I'm missing things and not explaining: it's been a while, especially sense I've messed with managed code. :P

trying to change that but having trouble finding documentation on C++ and managed code... I've been able to figgure it out but it is time consuming...
(and you see... I never specifically learned compiler theory and things like exactly what the heap is... >.> I just know code and what I need to troubleshoot compiling...)
I forget where I found this snippet, but putting:

#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif


at the top of a .cpp file will make msvc spit out where the memory that leaked was allocated, if it was allocated from that .cpp file.

This topic is closed to new replies.

Advertisement