Sign in to follow this  
daniel_i_l

memory leaks

Recommended Posts

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
I forget where I found this snippet, but putting:


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static 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.

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