线程提前终止代码255

Thread Terminating Early with Code 255

本文关键字:代码 终止 线程      更新时间:2023-10-16

我正在尝试在线程中运行我的程序的一部分,并得到一个不寻常的结果。

我已经用Remus建议的更改结果更新了这个问题,但由于我仍然得到一个错误,我觉得这个问题仍然是开放的。

我已经在一个dll中实现了一些功能,以绑定到一个供应商的软件中。一切工作,直到我试图在这个dll中创建一个线程。

这是DLL的相关部分:

extern "C" {
__declspec(dllexport) void __cdecl ccEntryOnEvent(WORD event);
}

定义供应商的软件调用的函数,然后:

using namespace std;
HANDLE LEETT_Thread = NULL;
static bool run_LEETT = true;
unsigned threadID;
void *lpParam;
int RunLEETTThread ( void ) {
    LEETT_Thread = (HANDLE)_beginthreadex( NULL, 0, LEETT_Main, lpParam, 0 , &threadID );
    //LEETT_Thread = CreateThread ( NULL, 0, LEETT_Main, lpParam, 0 , NULL );
    if ( LEETT_Thread == NULL ) 
        ErrorExit ( _T("Unable to start translator thread") );
    run_LEETT = false;  // We only wish to create the thread a single time.
    return 0;
}
extern "C" void __cdecl ccEntryOnEvent(WORD event ) {
    switch (event) {
    case E_START:
        if ( run_LEETT ) {
            RunLEETTThread ();
            MessageText ( "Running LEETT Thread" );
        }
        break;
    }
    WaitForSingleObject( LEETT_Thread ,INFINITE);
    return;
}  
函数声明为
unsigned __stdcall LEETT_Main ( void* lpParam ) {

LEETT_Main在没有优化的情况下编译为独立可执行文件时约为136k(我有一个单独的文件,其中包含main(),调用与myFunc相同的函数)。

在改变调用线程的方式之前,当声明一个包含std::list的结构时,程序会崩溃,如下所示:

struct stateFlags {
    bool inComment;    // multiline comments bypass parsing, but not line numbering
    // Line preconditions
    bool MCodeSeen;  // only 1 m code per block allowed
    bool GCodeSeen;  // only 1 g code per block allowed
    std::list <int> gotos; // a list of the destination line numbers
};

它现在在_beginthreadx命令上崩溃了,通过跟踪显示如下

    /*
     * Allocate and initialize a per-thread data structure for the to-
     * be-created thread.
     */
    if ( (ptd = (_ptiddata)_calloc_crt(1, sizeof(struct _tiddata))) == NULL )
            goto error_return;
通过跟踪,我看到了一个错误252(坏ptr)和255(运行时错误)。

我想知道是否有人遇到过这种行为创建线程(在dll ?)和补救措施可能是什么。当我在我的小程序中创建这个结构的实例时,没有任何问题。当我删除list变量时,程序只是在声明字符串

时在其他地方崩溃了。

在这一点上,我非常愿意接受建议,如果我必须的话,我现在会删除线程的想法,尽管它不是特别实用。

谢谢,特别是那些再次阅读这篇文章的人:)

使用CRT (std::list意味着CRT)的线程需要用_beginthreadx创建,如MSDN上的文档所示:

可执行程序中调用C运行时库(CRT)的线程应该为线程使用_beginthreadx和_endthreadx函数吗管理而不是CreateThread和ExitThread;

不清楚你如何启动你的线程,但似乎你在DllMain中这样做,这是不推荐的(参见从DllMain创建线程会死锁吗?)。

在重新检查这里的注释和项目配置时,供应商提供的解决方案文件使用/MTd进行调试,但我们正在构建一个DLL,所以我需要使用/MDd,它立即编译并正确运行。

很抱歉,这是一个可笑的挠头…