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