◆◆多文档OnNewDocument中返回FALSE之后出现错误,请帮忙分析下。
步履人生 2011-09-07 10:17:31 BOOL CMyDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
CDlgConnection dlg(&m_dbConn);
if(dlg.DoModal()!=IDOK)
return FALSE;
if (!CDocument::OnNewDocument())
return FALSE;
return TRUE;
}
只要这个过程返回FALSE,程序立马崩溃,通过F11单步调试跟踪,在执行完该过程之后会到文件DOCMULTI.CPP文件151行的pFrame->DestroyWindow();继续跟踪到文件WINMDI.CPP的过程:
BOOL CMDIChildWnd::DestroyWindow()
{
if (m_hWnd == NULL)
return FALSE;
// avoid changing the caption during the destroy message(s)
CMDIFrameWnd* pFrameWnd = GetMDIFrame();
HWND hWndFrame = pFrameWnd->m_hWnd;
ASSERT(::IsWindow(hWndFrame));
DWORD dwStyle = SetWindowLong(hWndFrame, GWL_STYLE,
GetWindowLong(hWndFrame, GWL_STYLE) & ~FWS_ADDTOTITLE);
MDIDestroy(); // 这里继续进入跟踪
if (::IsWindow(hWndFrame))
{
ASSERT(hWndFrame == pFrameWnd->m_hWnd);
SetWindowLong(hWndFrame, GWL_STYLE, dwStyle);
pFrameWnd->OnUpdateFrameTitle(TRUE);
}
return TRUE;
}
在执行到MDIDestroy();之后,继续跳入跟踪到AFXWIN2.INL文件中的937行,如下:
_AFXWIN_INLINE void CMDIChildWnd::MDIDestroy()
{ ASSERT(::IsWindow(m_hWnd)); ::SendMessage(GetParent()->m_hWnd, WM_MDIDESTROY, (WPARAM)m_hWnd, 0L); }
然后在这里就直接挂掉了,通过跳入IsWindow跟踪,m_hWnd也是有值的。弹出的对话框是“Unhandled exception in My.exe (MSVCRTD.DLL): 0xC0000005: Access Violation.”
Output调试窗口最后3行是:
CDocument::OnNewDocument returned FALSE.
First-chance exception in My.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
First-chance exception in My.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
First-chance exception in My.exe (MSVCRTD.DLL): 0xC0000005: Access Violation.
请帮忙分析一下,可能出现的原因,如何修正?
因为如果OnNewDocument返回TRUE的时候,然后在通过菜单或X按钮关闭子文档是一点问题都没有的。