创建新的CMFCDesktopAlertWnd时,何时删除已分配的内存?

时间:2012-05-29 20:30:18

标签: c++ mfc

如何创建桌面提醒的大多数示例如下所示。它使用new关键字创建一个新的CMFCDesktopAlertWnd,用于创建CMFCDesktopAlertDialog。这些示例都没有删除函数末尾的对象,所以我想是有些东西正在释放内存。是什么解除了记忆?

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));
   }
}

2 个答案:

答案 0 :(得分:1)

您可以查看源代码:

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

当窗口被销毁时,WM_NCDESTROY消息将删除分配的内存。

答案 1 :(得分:0)

Microsoft suggests删除它属于PostNcDestroy()而不是OnNcDestroy()方法,我一直在看到comctl32!ComboBox_WndProc()试图引用释放内存的一些崩溃删除过早,即在OnNcDestroy().