【发布时间】:2011-05-17 23:22:55
【问题描述】:
我的应用程序在退出线程函数时崩溃。这就是我的线程的初始化方式:
LPTHREAD_START_ROUTINE pThreadStart = (LPTHREAD_START_ROUTINE)NotifyWindowThreadFn;
void * pvThreadData = reinterpret_cast<void *>(_pobjSerialPort);
// Create the exit notify window thread event handle.
_hNotifyWindowThreadExitEvent = ::CreateEvent(
NULL, // No security
TRUE, // Create a manual-reset event object
FALSE, // Initial state is non-signaled
NULL // No name specified
);
if ( _hNotifyWindowThreadExitEvent == NULL )
{
TRACE(_T("CreateNotifyWindow : Failed to get a handle for the exit message-only window event.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
return ::GetLastError();
}
// Create the notify window thread to begin execution on its own.
_hNotifyWindowThread = ::CreateThread(
NULL, // No security attributes.
0, // Use default initial stack size.
pThreadStart, // Function to execute in new thread.
pvThreadData, // Thread parameters.
0, // Use default creation settings.
NULL // Thread ID is not needed.
);
if ( _hNotifyWindowThread == NULL )
{
TRACE(_T("CreateNotifyWindow : Failed to create handle for message-only window thread.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
return ::GetLastError();
}
这是我的线程函数中被执行的部分:
DWORD NotifyWindowThreadFn( void * pParam )
{
static CNotifyWindow * pobjNotifyWindow = NULL;
CSerialPort * pobjSerialPort = reinterpret_cast<CSerialPort *>(pParam);
// Create notify window to handle surprize removal/insertion events...
try
{
pobjNotifyWindow = new CNotifyWindow();
}
catch ( DWORD error )
{
return error; // 1. PC gets here
}
catch ( long error )
{
return error;
}
catch ( ... )
{
return ERROR_CANNOT_MAKE;
}
/* Other stuff that is not executed due to return. */
} // 2. PC then gets here
当应用程序崩溃时,Visual Studio 会给我这个错误消息:
Windows 已在 CppTestConsole.exe 中触发断点。
这可能是由于堆损坏,这表明 CppTestConsole.exe 或其已加载的任何 DLL 中存在错误。
这也可能是由于用户在 CppTestConsole.exe 获得焦点时按 F12。
输出窗口可能有更多的诊断信息。
输出窗口没有什么特别有用的东西。只有……
线程“NotifyWindowThreadFn”(0x414) 已退出,代码为 0 (0x0)。
然后它显示一堆DLL被卸载。当我单击 Break 按钮时,PC 位于 dbgheap.c 中 _CrtIsValidHeapPointer 的末尾。有没有人知道为什么我的应用程序在线程退出时崩溃?我不应该直接从线程函数中返回吗?谢谢。
【问题讨论】:
标签: c++ multithreading visual-studio-2008 crash heap-memory