Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
how do i detect memory leaks When the CRT and MFC libraries are linked?
Posted

Solution 3 should be your first stop (using the built-in debugging methods)...

Here's the information on how to use it straight from the source (MSDN):
http://msdn.microsoft.com/en-us/library/7sx52ww7.aspx[^]
 
Share this answer
 
Finding memory leaks is difficult. The basic nature of the problem is that how can you tell if the process is done with a piece of memory that has been allocated. This is the magic of memory managers in C# and Java (which have the benefit of IL and Javabyte code respectively). The garbage collector makes a determination about whether a variable has gone out of scope and deallocates/frees that memory. There are a lot of tools to help you determine that from your code. Here is a link to one of them:

http://deleaker.com/

Without the source code, it would be difficult to analyze. I hope this helps.
 
Share this answer
 
v2
MFC already contains a memory leak detection mechanism. Visual studio generates a couple of header lines in your code that replace in DEBUG builds the calls to the new operator with a call to a debug-new function, which records every single memory allocation and the place it comes from. At the end of the run, when all allocations made by new should have been given back by a corresponding call to delete, MFC outputs a list in the output window of all those allocations that are still open.

This is not the most sophisticated memory leak tracker, but it works quite well in many situations. Just learn to interpret that list and you get quite a long way.
 
Share this answer
 
v2
Comments
H.Brydon 26-Dec-13 15:59pm    
Not sure why you were downvoted. Your solution is correct (except that DEBUG_NEW is misspelled). More details are provided in Solution #4.

One thing to watch out for is to make sure you match new/delete and new[]/delete[] as this can upset the leak detection report.

+5 to help set the universe straight again.
nv3 27-Dec-13 14:15pm    
Thanks Harvey!
You can use visual leak detector in link
https://vld.codeplex.com/[^]
 
Share this answer
 
This is not a Good Question, try to Google. Please FAQ read before post a any type of question Code Project Quick Answers FAQ[^]
 
Share this answer
 
There is no direct way of identifying memory leaks, even if there are tools available it is very little that they can do for you.Best is follow this approach
Launch Process Explorer and montior "Private Bytes", "Handles" for your process.
Now start executing the use cases which gives you memory leak.Now observe what is growing if Private Bytes is growing then you have allocated memory using new or malloc but not yet released.
If handle count is going up then you have not released certain handles.
Just look into that area of code and fix the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900