int CDlgTest::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
m_pFrameWnd = new CFrameWnd();
m_pFrameWnd->Create(strMyClass, "", WS_CHILD, CRect(0, 0, 300, 300), this);
}
我们习惯性创建了就去对话框的析构函数里delete,但是MFC关闭对话框会报异常,原因是关闭对话框时会触发PostNcDestroy虚函数,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;
}
同理有CView的继承类,MFC框架希望我们是通过框架的方式去创建,由框架自动析构
而不是手动去new
有时间的话可以去理解一下RUNTIEM_CLASS机制,用宏实现的
它就是用来实现创建CObject派生类对象
CRuntimeClass *pClass = RUNTIME_CLASS(CTest5View);
CView *p = (CView *)pClass->CreateObject();
这个机制对于笔者现在来说看看就懂了,就是一些难看的宏组成,侯杰称为MFC的六大神器之一。但是笔者总觉得不够优美,对于初学者来说不够友好。