Mr. Boy
Mr. Boy

Reputation: 63816

MFC: Deleting dynamically created CWnd objects

Lets say in a dialog, we dynamically create a variable number of CWnds... like creating a and registering a CButton every time the user does something/

Some pseudo-code...

class CMyDlg : public CDialog
{
 vector<CWnd *> windows;

 void onClick()
 {
  CButton *pButton = new CButton(...);
  //do other stuff like position it here
  windows.push_back(pButton);
 }
}

Do I need to explicitly delete them or will MFC do it? If I have to, would it be in the destructor as normal, or are there any special things to avoid breaking MFC... making sure I don't delete the objects while the HWNDs are still in use for example?

Upvotes: 3

Views: 5705

Answers (1)

aJ.
aJ.

Reputation: 35470

CButton *pButton = new CButton(...);

These are C++ objects, which needs to be deleted explicitly. (Where as Main frame windows and Views are self destructed).

You can refer the detailed answer ( by me) Destroying Window Objects

Upvotes: 3

Related Questions