void MyDlg::OnDestroy()
{
CDialog::OnDestroy();
delete this;
}Warning: calling DestroyWindow in CDialog::~CDialog --
OnDestroy or PostNcDestroy in derived class will not be called.
如果用下面的代码就没有警告:
void MyDlg::PostNcDestroy()
{
delete this;
}请各位讲讲其中的道理。
{
CDialog::OnDestroy();
delete this;
}Warning: calling DestroyWindow in CDialog::~CDialog --
OnDestroy or PostNcDestroy in derived class will not be called.
如果用下面的代码就没有警告:
void MyDlg::PostNcDestroy()
{
delete this;
}请各位讲讲其中的道理。
解决方案 »
- Richedit 中如何正确获取ActiveX控件的位置
- 如何将字符串 保存到 IStream?
- 在VCKbase上下了一个picViewer程序,编译时碰到这样一个问题:描述如下,already defined. Cannot add 61446(0xF006)
- ISAPI Filter 的问题
- 求用DCOM/COM+进行客户端与服务器之间通信的例子!!!
- 当程序关闭的时候.有一个辅助线程有还有一个Socket 在listening ,请问如何结束它而不造成内存泄漏
- 请问一个在控件中画图的语法错误?
- 为什么Release版本不能正确运行?
- VS2110的问题帮帮忙啊!!
- 如何控制打印机退纸,此问题困扰我好长时间了,求助!
- 急,求一实例讲解怎样用ldap_search()函数搜索本地机在活动目录中的注册信息(100分)
- 编译路径的小问题??请高人指点
MyDlg::~MyDlg()
{
delete this;
}
CDialog::~CDialog()
{
if (m_hWnd != NULL)
{
TRACE0("Warning: calling DestroyWindow in CDialog::~CDialog --\n");
TRACE0("\tOnDestroy or PostNcDestroy in derived class will not be called.\n");
DestroyWindow();
}
}
他的意思是,如果对话框对象没有通过正常渠道结束,那么他的OnDestroy等函数将没有机会调用。
比如 CMyDialog* pDlg = new CMyDialog;
pDlg ->Create(***);
pDlg ->ShowWindow(***);
delete pDlg; 就可能引起上面情况的发生,因为析构函数将被调用,OnDestroy等将得不到清除资源的机会。
那在哪里清楚堆上分配的资源,调用delete呢?一般是在CDialog::PostNcDestroy函数中:
void MyDlg::PostNcDestroy()
{
delete this;
}
这样,保证对话框自己清除在堆上new出来的资源,而不是我们在对话框外面清除,这样避免资源得不到回收。一般,引起对话框关闭的操作从点关闭按钮开始,WM_SYSCOMMAND -> WM_CLOSE -> WM_DESTROY -> WM_NCDESTROY ... 最后PostNcDestroy中delete this;
void MyDlg::PostNcDestroy()
{
delete this;
}
否则会出现违规操作。比如在全局MyDlg dlg;对象。