应用程序崩溃线程退出 - C ++

时间:2010-11-30 15:59:35

标签: c++ multithreading visual-studio-2008 crash heap

我的应用程序在退出线程函数时崩溃。这是我的线程初始化的方式:

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的末尾。当线程退出时,有没有人对我的应用程序崩溃的原因有任何想法?我不应该直接从线程函数中返回吗?感谢。

3 个答案:

答案 0 :(得分:3)

我可能错了,但似乎你正在尝试从工作线程创建一个窗口。不要这样做。 Windows需要消息泵才能运行,并且应用程序中只有一个消息泵 - 它位于主线程中。

答案 1 :(得分:1)

你应该声明并定义你的函数:DWORD WINAPI NotifyWindowThreadFn(void * pParam)

答案 2 :(得分:0)

尝试使用_beginthreadex代替CreateThread:

  

可执行文件中调用的线程   C运行时库(CRT)应该   使用_beginthreadex和   _endthreadex用于线程管理而不是CreateThread   和ExitThread;这需要使用   多线程版本的   CRT。如果使用创建的线程   CreateThread调用CRT,即CRT   可以终止该过程   低内存条件。

相关问题