BobbyA
BobbyA

Reputation: 2260

When creating a new CMFCDesktopAlertWnd, when is the allocated memory deleted?

Most examples of how to create a desktop alert look like the below. It uses the new keyword to create a new CMFCDesktopAlertWnd that's used to create a CMFCDesktopAlertDialog. None of these examples delete the object at the end of the function, so I imagine something is deallocating the memory. What's deallocating the memory?

CMFCDesktopAlertWnd* pPopup = new CMFCDesktopAlertWnd;
// int m_nAnimation
pPopup->SetAnimationType ((CMFCPopupMenu::ANIMATION_TYPE) m_nAnimation);

// int m_nAnimationSpeed
pPopup->SetAnimationSpeed (m_nAnimationSpeed);

// int m_nTransparency
pPopup->SetTransparency ((BYTE)m_nTransparency);

// BOOL m_bSmallCaption
pPopup->SetSmallCaption (m_bSmallCaption);

// BOOL m_bAutoClose, int m_nAutoCloseTime
pPopup->SetAutoCloseTime (m_bAutoClose ? m_nAutoCloseTime * 1000 : 0);

// int m_nPopupSource
if (m_nPopupSource == 0)
{
   // int m_nVisualMngr
   // CPoint m_ptPopup
   // The this pointer points to a CDesktopAlertDemoDlg class which extends the CDialogEx class.
   if (m_nVisualMngr == 5)  // MSN-style
   {
      pPopup->Create (this, IDD_DIALOG2, NULL, m_ptPopup, RUNTIME_CLASS (CMSNDlg));
   }
   else
   {
      pPopup->Create (this, IDD_DIALOG1, 
      m_menuPopup.GetSubMenu (0)->GetSafeHmenu (), m_ptPopup, RUNTIME_CLASS (CMyPopupDlg));
   }
}

Upvotes: 1

Views: 720

Answers (2)

user3232533
user3232533

Reputation: 1

Microsoft suggests the delete this belongs in the PostNcDestroy() rather than the OnNcDestroy() method, and I've been seeing some crashes in which comctl32!ComboBox_WndProc() is trying to reference freed memory when the delete is done too early, i.e., in the OnNcDestroy().

Upvotes: 0

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6050

You could look at the source code:

void CMFCDesktopAlertWnd::OnNcDestroy()
{
    CWnd::OnNcDestroy();
    delete this;
}

When the window is destroyed, the WM_NCDESTROY message deletes the allocated memory.

Upvotes: 1

Related Questions