Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFC: Deleting dynamically created CWnd objects

Tags:

c++

mfc

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?

like image 316
Mr. Boy Avatar asked Nov 20 '25 18:11

Mr. Boy


1 Answers

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

like image 107
aJ. Avatar answered Nov 23 '25 07:11

aJ.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!