in Programming

VS Memory leaks (RichEdit2.0) and Access violation

Summary

When develop on a Windows application, solved memory leaks issue on RichEdit2.0, and pointer access violation exception.

Memory leaks (RichEdit2.0)

It is very common to encounter memory leaks when using RichEdit2.0 in Visual Studio, as this RichEdit2.0 requires to be initialized. The error displays as below,

Detected memory leaks!
Dumping objects ->
f:ddvctoolsvc7libsshipatlmfcsrcmfcoccmgr.cpp(195) : {335} normal block at 0x00FDAA00, 80 bytes long.
 Data: <                > 01 00 00 00 00 00 00 00 FF FF FF FF 00 00 00 00 
f:ddvctoolsvc7libsshipatlmfcsrcmfcoccmgr.cpp(181) : {334} normal block at 0x032E1F88, 44 bytes long.
 Data: <                > FF FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 
Object dump complete.

So add the RichEdit2.0 initialize code as below,

 //for RichEdit2.0
 if (!AfxInitRichEdit2())
 {
  msg = _T("Error in RichEdit 2.0 initialization!");
  MessageBox(NULL, msg, _T("Fatal"), MB_ICONSTOP|MB_OK);
 }

Above code must be added before the Dialog Windows was showed, as below code,

  CAPDURunDlg dlg;
  m_pMainWnd = &dlg;
  INT_PTR nResponse = dlg.DoModal();
  if (nResponse == IDOK)
  {
   // TODO: Place code here to handle when the dialog is
   //  dismissed with OK
  }
  else if (nResponse == IDCANCEL)
  {
   // TODO: Place code here to handle when the dialog is
   //  dismissed with Cancel
  }

This memory leak issue resolved.

Access violation

Encountered below issue unhandled exception issue when exit the application, it caused by additional destroyed the instances, i.e. I destroyed the instance twice when exit the application.

Unhandled exception at 0x78d2b4ec (mfc90ud.dll) in APDURun.exe: 0xC0000005: Access violation reading location 0xfeeefeee.

The CLogger was destroyed as below code when exit,

BOOL CAPDURunDlg::DestroyWindow()
{
 // TODO: Add your specialized code here and/or call the base class
 //CLogger::DestroyInstance();
 return CDialog::DestroyWindow();
}

The same instance was also destroyed at below code,

int CAPDURunApp::ExitInstance()
{
 // TODO: Add your specialized code here and/or call the base class
 CLogger::DestroyInstance();
 return CWinAppEx::ExitInstance();
}

Delete the instance destroy code at one of these two places, problem resolved.

Write a Comment

Comment