Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Dll Cleanup Bug

Dll Cleanup Bug

Scheduled Pinned Locked Moved Clever Code
helpquestion
5 Posts 4 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joe Woodbury
    wrote on last edited by
    #1

    The following code is from a DLL. The thread in question had allocated resources. It has been simplified but assume the following: hThread is a running thread hTerminate is an event that signals the thread to terminate

    BOOL APIENTRY DllMain(
    HMODULE,
    DWORD reasonForCall,
    LPVOID)
    {
    if (reasonForCall == DLL_PROCESS_DETACH)
    SetEvent(hTerminate);
    if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT)
    TerminateThread(hThread, 0);
    CloseHandle(hThread);
    CloseHandle(hTerminate);
    break;
    }
    return TRUE;
    }

    Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

    M S M 3 Replies Last reply
    0
    • J Joe Woodbury

      The following code is from a DLL. The thread in question had allocated resources. It has been simplified but assume the following: hThread is a running thread hTerminate is an event that signals the thread to terminate

      BOOL APIENTRY DllMain(
      HMODULE,
      DWORD reasonForCall,
      LPVOID)
      {
      if (reasonForCall == DLL_PROCESS_DETACH)
      SetEvent(hTerminate);
      if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT)
      TerminateThread(hThread, 0);
      CloseHandle(hThread);
      CloseHandle(hTerminate);
      break;
      }
      return TRUE;
      }

      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Doing anything like that in DllMain()is a Bad Thing. The WaitForSingleObject() will always timeout because it is being called while the loader lock is held, which prevents the other thread from exiting.

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

      J 1 Reply Last reply
      0
      • M Michael Dunn

        Doing anything like that in DllMain()is a Bad Thing. The WaitForSingleObject() will always timeout because it is being called while the loader lock is held, which prevents the other thread from exiting.

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        I posted this because I've seen this many times. Many developers don't notice because the leak or timeout happens when the entire process is exiting. I consider it one of the major design bugs in Win32.

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        1 Reply Last reply
        0
        • J Joe Woodbury

          The following code is from a DLL. The thread in question had allocated resources. It has been simplified but assume the following: hThread is a running thread hTerminate is an event that signals the thread to terminate

          BOOL APIENTRY DllMain(
          HMODULE,
          DWORD reasonForCall,
          LPVOID)
          {
          if (reasonForCall == DLL_PROCESS_DETACH)
          SetEvent(hTerminate);
          if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT)
          TerminateThread(hThread, 0);
          CloseHandle(hThread);
          CloseHandle(hTerminate);
          break;
          }
          return TRUE;
          }

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          I'd guess it's a loader lock[^] problem.

          Steve

          1 Reply Last reply
          0
          • J Joe Woodbury

            The following code is from a DLL. The thread in question had allocated resources. It has been simplified but assume the following: hThread is a running thread hTerminate is an event that signals the thread to terminate

            BOOL APIENTRY DllMain(
            HMODULE,
            DWORD reasonForCall,
            LPVOID)
            {
            if (reasonForCall == DLL_PROCESS_DETACH)
            SetEvent(hTerminate);
            if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT)
            TerminateThread(hThread, 0);
            CloseHandle(hThread);
            CloseHandle(hTerminate);
            break;
            }
            return TRUE;
            }

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            M Offline
            M Offline
            Monty2
            wrote on last edited by
            #5

            Excellent i didn't know that... thanks to all of you who replied :)


            If you think you can than you can, if you think you can't you are right.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups