Thread: tools for finding memory leaks

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    tools for finding memory leaks

    I have previously posted on memory leak in a MFC application - microsoft.public.vc.mfc | Google Groups about a memory leak problem that I had trouble finding and solving. To summarize, the two projects are posted in these two links, although you may not have the required libraries and data files to build or run them properly:

    usensimcalc: https://rcpt.yousendit.com/671032497...f59d5fd08a21e6
    rendering: https://rcpt.yousendit.com/671035295...f194240615586e

    I tried using {,,msvcr80d.dll}_crtBreakAlloc and setting its number to a number printed on the {} brackets in the debug output on the memory leak information on the memory allocation number and memory address. Also the memory allocation number in the {} brackets are different every time I run the splitter project.

    As suggested in one of the links in my original post, I tried using CMemoryState objects provided by MFC. Although it confirms that DoNeedle function is responsible for some leak instances, the memory dumping function doesn't tell me enough information that I understand what's happening. Below is how I used the MFC CMemoryState objects:

    Code:
    		// run the simulation only when the flag is true
    		// later, modify it by having a start button message handler modifying its status
    		if(startsim && needlemodel)
    		{
    			theMesh->NodesRepoint(needlemodel->Xall);
    			theMesh->nodesdeleteignore = true;		// change this parameter's scope back to private
    			startsim = false;
    		}
    
    		if (theMesh->nodesdeleteignore && 
    			needlemodel)
    		{
    			if (count < needlemodel->needleBasePos.entries)
    			{
    #if _DEBUG
    				CMemoryState oldstate, newstate, diffstate;
    				oldstate.Checkpoint();
    #endif
    				
    				// Note: memory leak indeed comes from this function
    				// need to determine the meaning of the memory leak diagnostics
    				needlemodel->DoNeedle(&(needlemodel->needleBasePos.nodepts[6*count]));
    
    #if _DEBUG
    				newstate.Checkpoint();
    				if(diffstate.Difference(oldstate, newstate))
    				{
    					AfxMessageBox("Memory Leak Detected");
    					diffstate.DumpAllObjectsSince();
    				}
    #endif
    According to the last suggestion in the post, I have tried using try catch block to see if exceptions are being caught by the program. Unfortunately, it didn't catch any exceptions from the MFC classes. Below is how I tried to use the try catch block:

    Code:
    				try
    				{
    					needlemodel->DoNeedle(&(needlemodel->needleBasePos.nodepts[6*count]));
    				}
    				catch (CMemoryException* e)
    				{
    					delete needlemodel;
    					exit(1);
    				}
    				catch (CFileException* e)
    				{
    					delete needlemodel;
    					exit(1);
    				}
    				catch (CException* e)
    				{
    					delete needlemodel;
    					exit(1);
    				}
    I'm not sure what other tools I can use to find the sources of the unexpected memory allocation. It would be great to hear some suggestions in terms of more ways of finding sources of memory allocation responsible for memory leaks. Again, I have already done my part to ensure new/delete or malloc/free are paired properly, with splitter (MFC document view application) containing memory leaks and usensimcalc (Windows console application) not containing memory leaks, while DoNeedle function in both projects are run in the exact same way.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The debug CRT has many functions that will assist you. I'm surprised that the allocation numbers were not the same in sequential runs. I've never had that problem.

    Regardless you can use _CrtMemCheckpoint() and _CrtMemDifference() to place checks around portions of code you suspect may be leaking and/or hogging memory. Check the compiler documentation for more debug CRT functions. There are quite a few of them. Note that the debug CRT as of MSVS 2005 (not sure about 2008) will report all Singletons as leaks since they are effectively never cleaned up till program exit.

    I've tried several third party apps to detect leaks and they all give far too many false positives.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I'm also surprised that they are not the same, making my debugging even more difficult. The CRT functions that you have suggested look similar to the CMemoryState functions that I have used in terms of how they work. Therefore, I'm not even sure if it makes things any easier with using them.

    I'm currently trying .NET memory profiler right now. I'm not sure about your impressions about this software, but I'm giving it a try.

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    I recommend Visual Leak Detector. It's been doing a great job for me.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Quote Originally Posted by Bubba View Post
    The debug CRT has many functions that will assist you. I'm surprised that the allocation numbers were not the same in sequential runs. I've never had that problem.

    Regardless you can use _CrtMemCheckpoint() and _CrtMemDifference() to place checks around portions of code you suspect may be leaking and/or hogging memory. Check the compiler documentation for more debug CRT functions. There are quite a few of them. Note that the debug CRT as of MSVS 2005 (not sure about 2008) will report all Singletons as leaks since they are effectively never cleaned up till program exit.

    I've tried several third party apps to detect leaks and they all give far too many false positives.
    Thanks for this suggestion. After using this method in a nested manner to pinpoint the source of the problem, I have managed to fix the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Memory Leaks?
    By jimmymax in forum C++ Programming
    Replies: 3
    Last Post: 11-23-2007, 03:57 AM
  3. Memory profiling and debugging tools
    By nickname_changed in forum Linux Programming
    Replies: 4
    Last Post: 09-14-2004, 03:58 PM
  4. COM Memory Leaks
    By subdene in forum Windows Programming
    Replies: 0
    Last Post: 06-07-2004, 11:57 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM