Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an MFC SDI app with a tree-view on the left (explorer-style).
I am trying to ensure there are no memory leaks and am using the following code to do so:
#ifdef _DEBUG
   		CMemoryState oldMemState, newMemState, diffMemState;
   		oldMemState.Checkpoint();
#endif
#ifdef _DEBUG
   		newMemState.Checkpoint();
   		if( diffMemState.Difference( oldMemState, newMemState ) )
   		{
			TRACE("Memory leaked!\n" );
      		AfxMessageBox(L"Memory leaked!\n" );
   		}
#endif

I am finding that I cannot get rid of memory leaks e.g. there is a memory leak between when I open a file that populates the tree-view (CTreeCtrl) and when I close the file and empty the tree-view. I delete all the tree items and call delete on all the pointers to the data structs held within each tree item, but there is still some memory leak somewhere. I call the open and close via MFC message handlers in the CMyDoc class. and I put the memory checks at the start of the OnOpenDoc method and at the end of the OnCloseDoc method.

I am wondering if there are unavoidable leaks when using MFC as there is stuff going on that I am not controlling.

Also, can anyone tell me which numbers to look at in the CMemeoryState struct to see how much memory is used at any time.

Is there a better (free) way to check for memory leaks in my case?
Posted
Updated 11-Dec-11 23:49pm
v2
Comments
LaxmikantYadav 12-Dec-11 6:51am    
Please share your code so we can help you.

you better look at the memory leaks of your code. Look in the content of the leaked memory. I guess that you dont delete all allocated sub-objects. You wont "call delete on all the pointers to the data structs" ;-)

In a tree you need to work recursly what needs a lot of care.
 
Share this answer
 
Comments
Jackie Lloyd 12-Dec-11 6:51am    
Yes, good point, but I had done that. However, now when I run the memory leak check it shows no memory leak! Before it was showing a very small memory leak. I don't know why it has changed now - a mystery to me, but at least it all seems happy now. Thankyou!
Getting the message "Memory leaked", doesnt mean that you have a memory leak. It means that only some difference is there in memory conumption at that time. When using MFC there are no unavoidable leaks, may be there is some memory consumption. for example see the below code

C++
CMemoryState new, old, diff;
old.CheckPoint();
m_csMyStr.LoadString( IDS_MYSTRING );
old.CheckPoint();
if( diff.Difference( old, new ))
{
    AfxMessageBox( "Mem Leak" );
}


It gives the message "Mem Leak". But is there any memory leak that the programmer needs to resove. No right?

but you need to make sure that what all you have newed, you just need to delete
If you debug your code, you can find the memory leak in visual studio IDE itself. In debug window.
 
Share this answer
 
Comments
Zarina-dev 30-Nov-22 21:51pm    
What to do if it doesn't show memory leak information in output window?

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