您的位置:首页 > 产品设计 > UI/UE

VS2010 MFC的小Bug-ASSERT(ContinueModal());

2015-09-07 10:55 1526 查看
CWnd::RunModalLoop

do
{
ASSERT(ContinueModal());

// pump message, but quit on WM_QUIT
if (!AfxPumpMessage())
{
AfxPostQuitMessage(0);
return -1;
}

// show the window when certain special messages rec'd
if (bShowIdle &&
(pMsg->message == 0x118 || pMsg->message == WM_SYSKEYDOWN))
{
ShowWindow(SW_SHOWNORMAL);
UpdateWindow();
bShowIdle = FALSE;
}

if (!ContinueModal())
goto ExitModal;

// reset "no idle" state after pumping "normal" message
if (AfxIsIdleMessage(pMsg))
{
bIdle = TRUE;
lIdleCount = 0;
}

} while (::PeekMessage(pMsg, NULL, NULL, NULL, PM_NOREMOVE));
}


在以上代码中

ASSERT(ContinueModal());

这个会判断 m_nFlags 的值来断言, 如果没有没有 WF_CONTINUEMODAL 属性将会报告异常, 但是循环有 PeekMessage

PeekMessage会进入内核的消息引擎会处理其他线程发送的消息

如果其他线程这时有 SendMessage(WM_CloseDlg); 而且据这个消息用 OnOK EndModalLoop之类的关闭了对话框, 就会修改m_nFlags 的值

这时就会出现断言, ASSERT(ContinueModal());

要避免这个问题把 SendMessage(WM_CloseDlg); 修改为 PostMessage, 让循环的 AfxPumpMessage 来处理消息, 避免 PeekMessage 来处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: