📄 subject_35882.htm
字号:
<p>
序号:35882 发表者:崔明亮 发表日期:2003-04-10 09:32:40
<br>主题:一个很奇怪的问题
<br>内容:我编写了以下程序:<BR>// class CMDIClientWnd<BR>class CMDIClientWnd : public CMDIChildWnd<BR>{<BR> DECLARE_DYNCREATE(CMDIClientWnd)<BR>public:<BR> CMDIClientWnd(); // 动态创建所使用的受保护的构造函数<BR> virtual ~CMDIClientWnd();<BR><BR>protected:<BR> DECLARE_MESSAGE_MAP()<BR>};<BR><BR>// class CMainFrame<BR>class CMainFrame : public CMDIFrameWnd<BR>{<BR> ...<BR><BR> // Attributes<BR>private:<BR> CMDIClientWnd *m_pMDIClient;<BR>};<BR><BR>// MainFrm.cpp<BR>CMainFrame::CMainFrame()<BR>{<BR> // TODO: add member initialization code here<BR> m_pMDIClient = new CMDIClientWnd;<BR>}<BR><BR>CMainFrame::~CMainFrame()<BR>{<BR> delete m_pMDIClient;<BR> m_pMDIClient = NULL;<BR>}<BR><BR>int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)<BR>{<BR> if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)<BR> return -1;<BR> <BR> if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP<BR> | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||<BR> !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))<BR> {<BR> TRACE0("Failed to create toolbar\n");<BR> return -1; // fail to create<BR> }<BR><BR> if (!m_wndStatusBar.Create(this) ||<BR> !m_wndStatusBar.SetIndicators(indicators,<BR> sizeof(indicators)/sizeof(UINT)))<BR> {<BR> TRACE0("Failed to create status bar\n");<BR> return -1; // fail to create<BR> }<BR> // TODO: Delete these three lines if you don't want the toolbar to<BR> // be dockable<BR> m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);<BR> EnableDocking(CBRS_ALIGN_ANY);<BR> DockControlBar(&m_wndToolBar);<BR><BR> m_pMDIClient->Create(NULL, // standard CMDIChildWnd class<BR> _T("My MDIChildWnd"), // caption of MDI child window<BR> WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW, // window styles<BR> rectDefault, // default rectangle size<BR> this); // parent window; can be NULL<BR><BR><BR> return 0;<BR>}<BR><BR>程序关闭的时候,老是有非法操作,但是如果把CMainFrame::~CMainFrame()中的语句去掉,则不会有错误,从理论上说,去掉delete会造成内存泄漏,但实际上却没有,我感到很疑惑,请各位高手解释一下原因。非常感谢!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:iwill 回复日期:2003-04-10 11:37:02
<br>内容:CMDIChildWnd派生自CFrameWnd,后者造PostNcDestroy中加入了 delete this;所以不必你手工删除了,只要窗口对象销毁,c++对象也自动销毁
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -