Memory leak of pOccDialogInfo when using CFormView in MFC

2009-06-20 18:45:36 Mingliang Dev

I am having a little investigaion on the Webbrowser control these days. I wrote a view class similar to the CHtmlView. The class is derived from the CFormView, and the Webbrowser control is created in the form. However, there is a memory leak reported for each instance of the view class. From the run log, the leaked memroy was allocated in the viewform.cpp file:

_AFX_OCC_DIALOG_INFO *pOccDialogInfo = new
  _AFX_OCC_DIALOG_INFO;

The pOccDialogInfo is not freed, which is quite confusing. After some searching job among the web, I found quite a lot of such memroy leak cases, all happened after inserting some OLE controls (including managed .net controls) into the FormView. Unfortunately, none of these cases run into a solution...

So I have try to solve the problem myself. Luckily after some experiments, the problem disappeared, while the solution was surprisingly simple: just modify the constructor of the CFormView derived class from

CXXView::CXXView()
: CFormView(CXXView::IDD)  // The line Memory leak happens - by Mingliang Dev
{
}

into

CXXView::CXXView()
: CFormView((LPCTSTR) NULL)
{
}

Then everything becomes fine. The cause of the problem is still unidentified, however.

View: Original article; From dev.mingliang.org.