在主对话框(CRolandViewDlg)中点击按钮打开一个新的对话框(CPlant),
void CRolandViewDlg::OnBnClickedViewplant()
{
hWnd_MyMainDlg = GetSafeHwnd(); //获取对话框的句柄
CPlant *plg;
plg = new CPlant();
if(plg != NULL)
{
BOOL ret = plg->Create(IDD_Plant,this);
if(!ret)
AfxMessageBox(_T("打开历史数据画面失败!"),0,0);
plg->ShowWindow(SW_SHOW);
}
else
AfxMessageBox(_T("打开历史数据画面失败!"),0,0);
}最后在新对话框中点击按钮返回:
void CPlant::OnBnClickedCancel()
{
OnCancel();
DestroyWindow();
}
同时重载了PostNcDestroy
void CPlant::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
}
结果发现,打开该新对话框后程序占用内存增加很多,但返回后内存却不减少,短时间再打开该对话框,占用内存不增加了,时间稍长再打开又增加了,照此以往,程序长期运行,内存岂不要耗尽?
请指教,是哪里出了问题呢,我的程序是在CE下运行的,请给一个完整的对话框打开,完毕的程序好吗?不会耗费内存的。
谢谢
void CRolandViewDlg::OnBnClickedViewplant()
{
hWnd_MyMainDlg = GetSafeHwnd(); //获取对话框的句柄
CPlant *plg;
plg = new CPlant();
if(plg != NULL)
{
BOOL ret = plg->Create(IDD_Plant,this);
if(!ret)
AfxMessageBox(_T("打开历史数据画面失败!"),0,0);
plg->ShowWindow(SW_SHOW);
}
else
AfxMessageBox(_T("打开历史数据画面失败!"),0,0);
}最后在新对话框中点击按钮返回:
void CPlant::OnBnClickedCancel()
{
OnCancel();
DestroyWindow();
}
同时重载了PostNcDestroy
void CPlant::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
}
结果发现,打开该新对话框后程序占用内存增加很多,但返回后内存却不减少,短时间再打开该对话框,占用内存不增加了,时间稍长再打开又增加了,照此以往,程序长期运行,内存岂不要耗尽?
请指教,是哪里出了问题呢,我的程序是在CE下运行的,请给一个完整的对话框打开,完毕的程序好吗?不会耗费内存的。
谢谢
解决方案 »
- 调试状态运行程序后再关闭程序,有如下内存问题,如何能知道是这个类里哪里出了问题呢?
- 新手求教:关于关闭进程的问题
- 谈谈你是怎么学习com的,小弟我最近看书看得很郁闷...
- CreateMutex为什么失败?
- 调用AfxSocketInit后,是否需要调用 WSACleanup ?
- 求助:LINK : fatal error LNK1561: entry point must be defined
- 写系统超时代码的问题。c高手进来拿分了。
- 关于“文件夹拷贝显示进度条问题”
- 怎么在任务栏 中显示程序的图标和名称
- 万分着急!!!一个关于Agent的问题(在线等,解决就给分)
- 有没有什么办法可以知道现在在执行哪个函数?
- 怎么用多个定时器模拟交通灯(红,黄,绿),大侠们看下我的问题
When you implement a modeless dialog box, always override the OnCancel member function and call DestroyWindow from within it. Don't call the base class CDialog::OnCancel, because it calls EndDialog, which will make the dialog box invisible but will not destroy it. You should also override PostNcDestroy for modeless dialog boxes in order to delete this, since modeless dialog boxes are usually allocated with new. Modal dialog boxes are usually constructed on the frame and do not need PostNcDestroy cleanup.
dlg.DoModal();
//不一定非要new
dlg.DoModal();
//不一定非要new这么创建的话,倒是方便,模态对话框
那么还需要重载PostNcDestroy吗?是不是退出对话框的时候直接这样就行呢?
void CPlant::OnBnClickedCancel()
{
OnOK();
}
创建模态对话框,退出时只用
OnOK();
也把PostNcDestroy()删掉了
结果退出后占用内存大小还是不会恢复,郁闷
void CPlant::OnBnClickedCancel()
{
OnCancel(); //这句去掉试试
DestroyWindow();
}
同时重载了PostNcDestroy
void CPlant::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
}
先看一下MSDN的原文:
When you implement a modeless dialog box, always override the OnCancel member
function and call DestroyWindow from within it. Don’t call the base class
CDialog::OnCancel, because it calls EndDialog, which will make the dialog box
invisible but will not destroy it. You should also override PostNcDestroy for
modeless dialog boxes in order to delete this, since modeless dialog boxes are
usually allocated with new. Modal dialog boxes are usually constructed on the
frame and do not need PostNcDestroy cleanup.
MS的指示:非模态对话框需要重载函数OnCanel,并且在这个函数中调用DestroyWindow。并且不能调用基类的OnCancel
,因为基类的OnCancel调用了EndDialog这个函数,这个函数是针对模态对话框的。
还有一个必须重载的函数就是PostNcDestroy,这也是一个虚函数,通常的非模态对话框是用类的指针,通过new创建
的,这就需要在PostNcDestroy函数中delete掉这个指针。
了解了理论过后,下面我们就可以用代码实现一下非模态对话框的创建和销毁过程:
//建立
//主框架中:
CTestDlg *pDlg=new CTestDlg;
pDlg->Create(IDD_TESTDLG,this);
pDlg->ShowWindow(SW_SHOW);
//对话框中:
void CTestDlg::OnCancel()
{
DestroyWindow();
}
void CTestDlg::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
}
如果要在点击按钮的情况下,销毁非模态对话框,只需要把按钮的事件映射到OnCancel函数即可。
一、模态对话框:
创建:
void CRolandViewDlg::OnBnClickedViewplant()
{
hWnd_MyMainDlg = GetSafeHwnd(); //获取对话框的句柄
CPlant plg;
plg.DoModal();
}
销毁:
void CPlant::OnBnClickedCancel()
{
::PostMessage(hWnd_MyMainDlg,WM_MYMSG_SaveUserIni,(WPARAM)1,0);
KillTimer(16);//16号定时器,显示动画
CDialog::OnCancel();
}
创建:
void CRolandViewDlg::OnBnClickedViewplant()
{
// TODO: 在此添加控件通知处理程序代码
hWnd_MyMainDlg = GetSafeHwnd(); //获取对话框的句柄
CPlant *plg;
plg = new CPlant();
if(plg != NULL)
{
BOOL ret = plg->Create(IDD_Plant,this);
if(!ret)
AfxMessageBox(_T("打开历史数据画面失败!"),0,0);
plg->ShowWindow(SW_SHOW);
}
else
AfxMessageBox(_T("打开历史数据画面失败!"),0,0);
}
新对话框中:
void CPlant::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
}
void CPlant::OnCancel()
{
CDialog::OnCancel();
DestroyWindow();
}
void CPlant::OnBnClickedCancel()
{
::PostMessage(hWnd_MyMainDlg,WM_MYMSG_SaveUserIni,(WPARAM)1,0);
KillTimer(16);//16号定时器,显示动画
OnCancel();
}