CDialog memory leak in VC10

We upgrade from VC8 to VC10 and discover a series of memory leaks that are apparently related to CDialog. The simplest example of this is demonstrated with the following code using CDialog, which has only a few buttons. In VC10 this is a leak, but in VC8 it is not:

for (int i = 0; i < 5000; ++i) { CDialog* dialog = new CDialog; dialog->Create(IDD_LEAKER, 0); dialog->DestroyWindow(); delete dialog; } 

Memory usage continues to grow, and in the example dialog we have about 30 leak buttons 10 s Mb.

Please note that the above is a test case in which we removed all of our code processing code, in our real code we have a derived class and use PostNcDestroy ().

Oddly enough, none of the following code examples flow in either VC8 or VC10:

 CDialog* dialog = new CDialog; for (int i = 0; i < 5000; ++i) { dialog->Create(IDD_LEAKER, 0); dialog->DestroyWindow(); } delete dialog; for (int i = 0; i < 5000; ++i) { CDialog* dialog = new CDialog; delete dialog; } 

What are we missing here?

+7
source share
1 answer

This is similar to how MFC manages its descriptor cards:

What is the life expectancy of CWnd obtained from CWnd :: FromHandle?

If you wait long enough for your application to stop working, you will return your memory, that is, in fact, this is not a leak. However, as you noticed, while Visual C ++ 2010 continues to consume more and more memory - until maps are displayed in OnIdle () - this does not seem to happen in Visual C ++ 2008.

Debugging an application containing your code shows that there are many more objects on the HWND temporary map in VC 10 than in VC 9.

The descriptor card code (winhand.cpp) does not seem to have changed between the two versions, but there is a lot of code in the MFC that uses it!

In any case, assuming that you really want to run your program, how is it, I think, you are working in some kind of automatic mode? - then you want to forcefully collect the trash bin at appropriate intervals. Take a look at this entry on MSDN:

http://msdn.microsoft.com/en-us/library/xt4cxa4e(v=VS.100).aspx

CWinThread :: OnIdle () actually calls this to order things:

 AfxLockTempMaps(); AfxUnlockTempMaps(/*TRUE*/); 
+7
source

All Articles