开发者

Thread Terminating Early with Code 255

开发者 https://www.devze.com 2023-03-20 01:06 出处:网络
I\'m attempting to run a part of my program in a thread and getting an unusual result. I have updated this question with the results of the changes suggested by Remus, but as I am still getting an er

I'm attempting to run a part of my program in a thread and getting an unusual result.

I have updated this question with the results of the changes suggested by Remus, but as I am still getting an error, I feel the question is still open.

I have implemented functionality in a dll to tie into a piece of vendor software. Everything works until I attempt to create a thread inside this dll.

Here is the relevant section of the DLL:

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

to define the function the vendor's software calls, then:

using namespace std;

HANDLE LEETT_Thread = NULL;
sta开发者_如何学Ctic 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;
}  

The function is declared as

unsigned __stdcall LEETT_Main ( void* lpParam ) {

LEETT_Main is about 136k when compiled as a stand alone executable with no optimization (I have a separate file with a main() in it that calls the same function as myFunc).

Prior to changing the way the thread is called, the program would crash when declaring a structure containing a std::list, shown here:

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

It now crashes on the _beginthreadex command, tracing through shows this

    /*
     * 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;

Tracing through this I saw a error 252 (bad ptr) and ultimately 255 (runtime error).

I'm wondering if anyone has encountered this sort of behaviour creating threads (in dlls?) and what the remedy might be. When I create an instance of this structure in my toy program, there was no issue. When I removed the list variable the program simply crashed elsewhere, on the declaration of a string

I'm very open to suggestions at this point, if I have to I'll remove the idea of threading for now, though it's not particularly practical.

Thanks, especially to those reading this over again :)


Threads that use CRT (and std::list implies CRT) need to be created with _beginthreadex, as documented on MSDN:

A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread;

Is not clear how you start your thread, but it appears that you're doing it in DllMain which is not recommended (see Does creating a thread from DllMain deadlock or doesn't it?).


In rechecking the comments here and the configuration of the project, the vendor supplied solution file uses /MTd for debug, but we are building a DLL, so I needed to use /MDd, which immediately compiles and runs correctly.

Sorry about the ridiculous head scratcher...

0

精彩评论

暂无评论...
验证码 换一张
取 消