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. Assertion error upon second run of thread

Assertion error upon second run of thread

Scheduled Pinned Locked Moved C / C++ / MFC
helpannouncement
8 Posts 2 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
    searcher08
    wrote on last edited by
    #1

    I started a thread to perform some decoding function. This is to circumvent the problem of the windows not being able to be updated while the decoding is in progress. The code runs perfect for the first run i.e. it went through the file selection process to the completion of the decoding process. However, when I want to decode a second file, an assetion failure occurred during or start of the decoding process. I narrowed down the bug to the 'decode' function in the thread function. Everything runs well second or third time with it commented. Now I know that it is this function causing the problem. This piece of function was running fine when separately called and I went through it many times. Appreciate if you could give advise on this issue. Any comment will be good. Snippets of my code: completed=false; AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,0,NULL); while(!completed) { // Perform the windows update. } UINT DecodeThreadProc(LPVOID Paramfilename) { decode((char *)Paramfilename); completed=true; return 0; } searcher08

    B 1 Reply Last reply
    0
    • S searcher08

      I started a thread to perform some decoding function. This is to circumvent the problem of the windows not being able to be updated while the decoding is in progress. The code runs perfect for the first run i.e. it went through the file selection process to the completion of the decoding process. However, when I want to decode a second file, an assetion failure occurred during or start of the decoding process. I narrowed down the bug to the 'decode' function in the thread function. Everything runs well second or third time with it commented. Now I know that it is this function causing the problem. This piece of function was running fine when separately called and I went through it many times. Appreciate if you could give advise on this issue. Any comment will be good. Snippets of my code: completed=false; AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,0,NULL); while(!completed) { // Perform the windows update. } UINT DecodeThreadProc(LPVOID Paramfilename) { decode((char *)Paramfilename); completed=true; return 0; } searcher08

      B Offline
      B Offline
      Blake Miller
      wrote on last edited by
      #2

      pFileName passed in to AfxBeinThreadProc must remain constant the entire time the thread is using the variable. I suspect it might have been local to some other function calling AfxBeginthread, so you got into trouble there - it was invalid by the time the thread executed and tried to read it.

      Any sufficiently gross incompetence is nearly indistinguishable from malice.

      S 1 Reply Last reply
      0
      • B Blake Miller

        pFileName passed in to AfxBeinThreadProc must remain constant the entire time the thread is using the variable. I suspect it might have been local to some other function calling AfxBeginthread, so you got into trouble there - it was invalid by the time the thread executed and tried to read it.

        Any sufficiently gross incompetence is nearly indistinguishable from malice.

        S Offline
        S Offline
        searcher08
        wrote on last edited by
        #3

        Thanks. The pFileName was not amended by any function after the AfxBeginThread was called. Can I amend it in the Thread program. I also noticed from the Memory usage monitor that the some memory was not released after the thread ended. I checked the code for errors in freeing pointers and there was no issue. Is there any way of releasing any memory used by the thread? The thread was running fine for the first time. The problem only arises when it was loaded the second time.

        B 1 Reply Last reply
        0
        • S searcher08

          Thanks. The pFileName was not amended by any function after the AfxBeginThread was called. Can I amend it in the Thread program. I also noticed from the Memory usage monitor that the some memory was not released after the thread ended. I checked the code for errors in freeing pointers and there was no issue. Is there any way of releasing any memory used by the thread? The thread was running fine for the first time. The problem only arises when it was loaded the second time.

          B Offline
          B Offline
          Blake Miller
          wrote on last edited by
          #4

          It does not matter if pFileNAme is amended or not, I was concerned its lifetime did not outlast the thread. Depending upon how a thread is called, especially in MFC, you might need to delete the CWinThread object after the thread has terminated, unless you set it to automatically delete itself.

          Any sufficiently gross incompetence is nearly indistinguishable from malice.

          S 1 Reply Last reply
          0
          • B Blake Miller

            It does not matter if pFileNAme is amended or not, I was concerned its lifetime did not outlast the thread. Depending upon how a thread is called, especially in MFC, you might need to delete the CWinThread object after the thread has terminated, unless you set it to automatically delete itself.

            Any sufficiently gross incompetence is nearly indistinguishable from malice.

            S Offline
            S Offline
            searcher08
            wrote on last edited by
            #5

            I did not do anything to the object. From the MSDN, I understand there is no requirement to end the thread as long as it is completed normally. I will heed your advise and proceed with the delete of the CWinThread Object. Is this what it is suppose to look like? CWinThread* ThreadHD = AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,0,NULL); delete(ThreadHD); ThreadHD=NULL; Many thanks for your advise. Greatly appreciated!

            B 1 Reply Last reply
            0
            • S searcher08

              I did not do anything to the object. From the MSDN, I understand there is no requirement to end the thread as long as it is completed normally. I will heed your advise and proceed with the delete of the CWinThread Object. Is this what it is suppose to look like? CWinThread* ThreadHD = AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,0,NULL); delete(ThreadHD); ThreadHD=NULL; Many thanks for your advise. Greatly appreciated!

              B Offline
              B Offline
              Blake Miller
              wrote on last edited by
              #6

              Yes, unless you set the auto delete member function. This is safer... CWinThread* ThreadHD = AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,**CREATE_SUSPENDED**,NULL); ThreadHD->m_bAutoDelete = TRUE; ThreadHD->ResumeThread(); This way, you don't have to wait for thread to exit before deleting ThreadHD yourself.

              Any sufficiently gross incompetence is nearly indistinguishable from malice.

              S 1 Reply Last reply
              0
              • B Blake Miller

                Yes, unless you set the auto delete member function. This is safer... CWinThread* ThreadHD = AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,**CREATE_SUSPENDED**,NULL); ThreadHD->m_bAutoDelete = TRUE; ThreadHD->ResumeThread(); This way, you don't have to wait for thread to exit before deleting ThreadHD yourself.

                Any sufficiently gross incompetence is nearly indistinguishable from malice.

                S Offline
                S Offline
                searcher08
                wrote on last edited by
                #7

                I tried the method as advised. However, an error occurred at the delete(ThreadHD) line. I think that if the thread has exited, there should not be a need to delete it. Please advise. Thanks. CWinThread* ThreadHD = AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL); ThreadHD->m_bAutoDelete = TRUE; ThreadHD->ResumeThread(); . . . delete(ThreadHD); ThreadHead=NULL;

                B 1 Reply Last reply
                0
                • S searcher08

                  I tried the method as advised. However, an error occurred at the delete(ThreadHD) line. I think that if the thread has exited, there should not be a need to delete it. Please advise. Thanks. CWinThread* ThreadHD = AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL); ThreadHD->m_bAutoDelete = TRUE; ThreadHD->ResumeThread(); . . . delete(ThreadHD); ThreadHead=NULL;

                  B Offline
                  B Offline
                  Blake Miller
                  wrote on last edited by
                  #8

                  The ThreadHD will delete itself! So you don't have to do it, that is why you got the assertion. You only need these lines... CWinThread* ThreadHD = AfxBeginThread(DecodeThreadProc,pfilename,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL); // m_bAutoDelete = TRUE allows a CWinThread to delete itself when the thread function exits ThreadHD->m_bAutoDelete = TRUE; ThreadHD->ResumeThread(); and REMOVE these lines from your code and the assertion will be gone... delete(ThreadHD); ThreadHead=NULL;

                  Any sufficiently gross incompetence is nearly indistinguishable from malice.

                  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