//programming windows with MFC, second edition 里面有很多例子是用MFC 1.0的形式写的。如下面的BOOL CMyApp::InitInstance ()
{
//
// Verify that the host system is running in a palettized video mode.
//
CClientDC dc (NULL);
if ((dc.GetDeviceCaps (RASTERCAPS) & RC_PALETTE) == 0) {
AfxMessageBox (_T ("Palette animation is not supported on this " \
"device. Set the color depth to 256 colors and try again."),
MB_ICONSTOP | MB_OK);
return FALSE;
} //
// Initialize the application as normal.
//
m_pMainWnd = new CMainWindow; //这里有new,但我发现不了有相关的delete,是不是有memory leak????
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
//我认为是有memory leak的,但是那本书,全部都是这样的,没有delete,难道是有错?
//谢谢!!!
解决方案 »
- 请教各位下面的代码为什么创建(notepad,记事本程序)进程失败了?
- Image报错
- 用LISTCTRL时出现一个很奇怪的问题,调用GETITEMTEXT时取出的是空,一个是对的,一个是错的
- CDC:BitBlt如何做镜像操作?
- vc中用到office和wps中的com组件,哪里有这些组件的方法的操作说明??
- 无法从“LRESULT (__thiscall CClientThread::* )(WPARAM,LPARAM)”转换为“void (__thiscall CWinThread::* )(WPARAM,LPARAM)”
- 很简单的问题,各位高人帮忙!
- 建立新IE进程,如何使IE脱机?
- 如何在XP下装98?(急)
- 关于网络编程的问题:
- cppunit的耦合测试
- 高分悬赏解决ts流分析问题
如果APP退出后,就应该释放啊。但自动释放是怎样呢? 为什么不在APP的析构函数里面delete???
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();在之后delete。他可能是在CMainWindow 的其他地方 delete this了。
那为什么代码里面没有delete?
主窗口你delete了,基本程序就退出了,操作系统会打扫战场的:)
void CFrameWnd::PostNcDestroy()
{
// default for frame windows is to allocate them on the heap
// the default post-cleanup is to 'delete this'.
// never explicitly call 'delete' on a CFrameWnd, use DestroyWindow instead
delete this;
}
MFC里面很多框架类都是auto_delete的。
一般都是在类本身的某个消息响应函数里面delete。把析构过程封装成消息驱动,从而达到更灵活更独立的效果。
当这些类的对象在栈上构建的时候,你就必须把该属性关闭,否则出问题!
如果没有该属性,纳米往往这些对象只能new出来用。上述Frame类应该就是这种。
pModalessDlg->Create(IDD_MODALESS,this);
pModalessDlg->ShowWindow(SW_SHOW);
m_pMainWnd是在基类CWinThread申明的,在CWinThread的析构函数中并没有调用delete m_pMainWnd;建议你把代码改为
CMainWindow mainWnd
m_pMainWnd = &mainWnd;
具体还要看看随书附赠的源代码,要看整个工程正常下
有new就肯定有delete
void CFrameWnd::PostNcDestroy()
{
// default for frame windows is to allocate them on the heap
// the default post-cleanup is to 'delete this'.
// never explicitly call 'delete' on a CFrameWnd, use DestroyWindow instead
delete this;
}
我看的与你的有点不同,我的是: programming windows with MFC