views:

34

answers:

1

I'm using a library (ANet) which is written natively in C and I am interfacing with it using C# via a small subset wrapper dll which i've made in C and statically links to it. The library (ANet) has a DllMain which simply tracks the number of references to it (so "Process attach" and "Thread attach" counts) but it seems to go into the negative (i.e. more detaches than attaches).

Here's ANet's DllMain. It's quite simple:

BOOL WINAPI DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
{
    static int procRefCount = 0;
    static int threadRefCount = 0;

    switch (fdwReason) {
    case DLL_PROCESS_ATTACH:
        procRefCount++;
        break;

    case DLL_PROCESS_DETACH:
        procRefCount--;
        break;

    case DLL_THREAD_ATTACH:
        threadRefCount++;
        break;

    case DLL_THREAD_DETACH:
        threadRefCount--;
        break;

    }

    if (procRefCount < 0) {
        MessageBox( NULL, "Bug - negative processes?", "DP DLL Error", MB_OK|MB_ICONERROR );
        return FALSE;
    }
    if (procRefCount > 1) {
        MessageBox( NULL, "Bug - too many processes trying to use DP", "DP DLL Error", MB_OK|MB_ICONERROR );
        return FALSE;
    }
    if (threadRefCount < 0) {
        MessageBox( NULL, "Bug - negative threads?", "DP DLL Error", MB_OK|MB_ICONERROR );
        return FALSE;
    }
    /* Only the first thread is allowed to join? */
    if (threadRefCount > 0) {
        //MessageBox( NULL, "Bug - too many threads trying to use DP", "DP DLL Error", MB_OK|MB_ICONERROR );
        return FALSE;
    }

    return(TRUE);
}

.. Which i'm aiming to not change so I don't have to recompile and redistribute this prebuilt library.
So since C# seems to randomly create and destroy threads, DllMain is called sporadically throughout my program. The thing is that the messagebox saying "Bug - negative threads?" will often also display!

Why would there be more THREAD_DETACH messages than THREAD_ATTACH messages?

+3  A: 

Because more threads exited during the lifetime of the DLL than were created during the lifetime of the DLL.

Threads don't remember which DLLs were present when they were created, so you get a detach message for every thread which exits after the DLL was loaded, but you only get attach messages for threads which are created after the DLL was loaded. So if you create n threads, load a DLL, then close those threads, the DLL sees n DLL_THREAD_DETACH messages but no DLL_THREAD_ATTACH messages.

It's up to the DLL to decide whether or not to do anything with the detached thread and to remember whether or not it has seen it before; the 'if' is important in 'If the DLL has stored a pointer to allocated memory in a TLS slot, it should use this opportunity to free the memory.' MSDN

Pete Kirkham
+1, very plausible.
Hans Passant
Good point, threads may have been created before the dll was loaded (even though it is on program load) which are removed later. That means it's normal operation, which is good, but it is still a difficult problem for me to solve
@zeiphon I don't know whether the library allows you to use it in a multiple threaded environment or not (the question mark in the comment in the source implies that the authors didn't know either). You might be able to hack it by calling DllMain(0,DLL_THREAD_ATTACH,0) yourself a few thousand times to increment the thread counter out of harm's way, but you also may need to ensure all calls into the DLL are from the same thread, which is trickier if you're using a .net GUI.
Pete Kirkham
@Pete Kirkham Thanks for the suggestion. You wouldn't have any information on the subject of restricting thread access to DLLs in .Net GUI programs which you could point me to? I don't believe the ANet library is at all thread-safe, and I may be using it inappropriately.