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. General Programming
  3. C / C++ / MFC
  4. Memory leak due to waiting thread

Memory leak due to waiting thread

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performancehelp
6 Posts 3 Posters 0 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.
  • S Offline
    S Offline
    Still learning how to code
    wrote on last edited by
    #1

    I have an application in which a second (worker) thread is created using AfxBeginThread().   The execution of this thread can be stalled due to a WaitOnSingleObject(), which MAY never get signalled.   In this situation, when the application main thread terminates, I get a memory leak as follows:- Detected memory leaks! Dumping objects -> strcore.cpp(118) : {78} normal block at 0x003011E0, 16 bytes long. Data: <                  D:\ > 01 00 00 00 03 00 00 00 03 00 00 00 44 3A 5C 00 strcore.cpp(118) : {77} normal block at 0x00301220, 59 bytes long. Data: <            .   Thre> 01 00 00 00 11 00 00 00 2E 00 00 00 54 68 72 65 thrdcore.cpp(166) : {71} client block at 0x003014C0, subtype 0, 112 bytes long. a CWinThread object at 003014C0, 112 bytes long If the WaitOnSingleObject gets signalled, then there is no memory leak. I have tried various things to get over this problem, but have had no success.   How should I properly deal with a waiting thread to avoid the memory leak ?   (Apologies if this is a "trivial" question to the more experienced "threaders" !!)

    Doug

    J 1 Reply Last reply
    0
    • S Still learning how to code

      I have an application in which a second (worker) thread is created using AfxBeginThread().   The execution of this thread can be stalled due to a WaitOnSingleObject(), which MAY never get signalled.   In this situation, when the application main thread terminates, I get a memory leak as follows:- Detected memory leaks! Dumping objects -> strcore.cpp(118) : {78} normal block at 0x003011E0, 16 bytes long. Data: <                  D:\ > 01 00 00 00 03 00 00 00 03 00 00 00 44 3A 5C 00 strcore.cpp(118) : {77} normal block at 0x00301220, 59 bytes long. Data: <            .   Thre> 01 00 00 00 11 00 00 00 2E 00 00 00 54 68 72 65 thrdcore.cpp(166) : {71} client block at 0x003014C0, subtype 0, 112 bytes long. a CWinThread object at 003014C0, 112 bytes long If the WaitOnSingleObject gets signalled, then there is no memory leak. I have tried various things to get over this problem, but have had no success.   How should I properly deal with a waiting thread to avoid the memory leak ?   (Apologies if this is a "trivial" question to the more experienced "threaders" !!)

      Doug

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

      Add a "Terminate" event and then do a WaitOnMultipleObjects() with the terminate event handle first in the array. When you need to terminate, set the terminate event and sleep at least 0 (to give the other thread a chance to run.) Have the thread then exit normally and it will do cleanup. A more sophisticated solution is to wait on the thread handle after you set the terminate event, though AfxBeginThread() makes this a bit tricky.

      S 1 Reply Last reply
      0
      • J Joe Woodbury

        Add a "Terminate" event and then do a WaitOnMultipleObjects() with the terminate event handle first in the array. When you need to terminate, set the terminate event and sleep at least 0 (to give the other thread a chance to run.) Have the thread then exit normally and it will do cleanup. A more sophisticated solution is to wait on the thread handle after you set the terminate event, though AfxBeginThread() makes this a bit tricky.

        S Offline
        S Offline
        Still learning how to code
        wrote on last edited by
        #3

        Hi Joe,   I had actually implemented your first solution already, except for the Sleep(0) - I NOW realise that there was a thread "race", which therefore still allowed failure.   I also found that it works just as well with the terminate event handle in the second position in the array - have I missed something ?   Many thanks for your help by the way !!

        Doug

        J 1 Reply Last reply
        0
        • S Still learning how to code

          Hi Joe,   I had actually implemented your first solution already, except for the Sleep(0) - I NOW realise that there was a thread "race", which therefore still allowed failure.   I also found that it works just as well with the terminate event handle in the second position in the array - have I missed something ?   Many thanks for your help by the way !!

          Doug

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

          I say put the terminate first because the wait processes objects in order. If the terminate event is last in the list and the previous events keep going signaled, you may never pick up that the terminate event is signaled (unless you check it after the WaitForMultipleObjects.)

          S 1 Reply Last reply
          0
          • J Joe Woodbury

            I say put the terminate first because the wait processes objects in order. If the terminate event is last in the list and the previous events keep going signaled, you may never pick up that the terminate event is signaled (unless you check it after the WaitForMultipleObjects.)

            S Offline
            S Offline
            Still learning how to code
            wrote on last edited by
            #5

            Of course !!   Thanks again for your help

            Doug

            R 1 Reply Last reply
            0
            • S Still learning how to code

              Of course !!   Thanks again for your help

              Doug

              R Offline
              R Offline
              Roger Stoltz
              wrote on last edited by
              #6

              Have a look here[^] for a more descriptive explanation of what Joe is talking about. You'll also find a lot of things that are Really Good To Know regarding multithreading in the article.

              "It's supposed to be hard, otherwise anybody could do it!" - selfquote
              "High speed never compensates for wrong direction!" - unknown

              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