c ++运行时检查失败#0 - ESP的值未正确保存...用于检查简单工作线程的点

时间:2014-06-13 16:03:47

标签: c++ mfc

我有一个对话框。在这个对话框:: OnInitDialog()我创建一个线程AfxBeginThread((AFX_THREADPROC)MyThreadProc,NULL);当我关闭运行时检查失败的对话框时它崩溃了,它指向thrdcore.cpp文件(Microsoft基础类C ++库)

// first -- check for simple worker thread DWORD nResult = 0; if (pThread->m_pfnThreadProc != NULL) { nResult = (*pThread->m_pfnThreadProc)(pThread->m_pThreadParams); ASSERT_VALID(pThread); }  我有一个代码来杀死线程OnClose函数,但它没有解决问题。有些帮助,我错过了什么?我的代码

HANDLE m_hExit;
DWORD dwResult = 0;
unsigned threadID = 0;
...


OnInitDialog()
{... 

m_hExit = (HANDLE)AfxBeginThread((AFX_THREADPROC)MyThreadProc, NULL);
}

OnClose()
{

    dwResult = WaitForSingleObject(m_hExit, 0);
if (dwResult == WAIT_TIMEOUT)
{
    printf("The thread is still running...\n");
}
else
{
    printf("The thread is no longer running...\n");
}
Sleep(10000);
dwResult = WaitForSingleObject(m_hExit, 0);
if (dwResult == WAIT_TIMEOUT)
{
    printf("The thread is still running...\n");
}
else
{
    printf("The thread is no longer running...\n");
}
CDialog::OnClose();

}

线程函数非常大(((

1 个答案:

答案 0 :(得分:4)

AfxBeginThreaddocumented,要求threadproc为

UINT __cdecl MyControllingFunction( LPVOID pParam );

您的评论说您的功能是

UINT WINAPI MyThreadProc( LPVOID pParam )

WINAPI定义为_stdcall(请参阅here

所以你的调用约定不匹配。正如其他人已经评论过的那样,演员是可疑的。实际上,这是您的代码编译的唯一原因。如果删除强制转换,编译器应显示错误。

解决方案是删除强制转换,然后修复函数的调用约定。一旦该代码在没有强制转换的情况下正确编译,它应该正确运行而不会破坏堆栈。