繁体   English   中英

使用代码 255 提前终止线程

[英]Thread Terminating Early with Code 255

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

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

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

这是DLL的相关部分:

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

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

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;
}  

function 被声明为

unsigned __stdcall LEETT_Main ( void* lpParam ) {

当编译为没有优化的独立可执行文件时,LEETT_Main 大约为 136k(我有一个单独的文件,其中包含一个 main(),它调用与 myFunc 相同的 function)。

在更改调用线程的方式之前,程序在声明包含 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
};

它现在在 _beginthreadex 命令上崩溃,跟踪显示这个

    /*
     * 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 中?)的行为以及可能的补救措施。 当我在我的玩具程序中创建此结构的实例时,没有问题。 当我删除列表变量时,程序只是在其他地方崩溃,在字符串的声明中

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

谢谢,尤其是那些再读一遍的人:)

使用 CRT(和std::list暗示 CRT)的线程需要使用_beginthreadex创建,如MSDN上所述:

调用 C 运行时库 (CRT) 的可执行文件中的线程应使用 _beginthreadex 和 _endthreadex 函数进行线程管理,而不是 CreateThread 和 ExitThread;

不清楚您是如何启动线程的,但似乎您在 DllMain 中执行此操作,这是不推荐的(请参阅是否从 DllMain 死锁创建线程? )。

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

对不起,可笑的挠头……

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM