Click to See Complete Forum and Search --> : Fail to end thread
weiwei
November 23rd, 2004, 01:52 AM
i wrote a multithread program, it works fine but there is application error when i exit my program.
i know which line of program cause the exception error but i don't know how to solve it
my code :
all the thread are CWinThread *
HANDLE hEndThread[9];
hEndThread[0] = m_pLoaderAThrd->m_hThread;
hEndThread[1] = m_pLoaderBThrd->m_hThread;
hEndThread[2] = m_pUnloaderAThrd->m_hThread;
hEndThread[3] = m_pUnloaderBThrd->m_hThread;
hEndThread[4] = m_pTransferThrd->m_hThread;
hEndThread[5] = m_pVisionThrd->m_hThread;
hEndThread[6] = m_pRobotThrd->m_hThread;
hEndThread[7] = m_pConveyorThrd->m_hThread;
hEndThread[8] = m_pLaserThrd->m_hThread;
// Wait for all scan threads to end , this line cause application error
DWORD dwRet = WaitForMultipleObjects(9,hEndThread, TRUE, 1000);
if (dwRet == WAIT_TIMEOUT)
TRACE ("SEQUENCE : Sequence time out to detects modules nded\n");
else
TRACE ("SEQUENCE : Sequence detects modules ended\n");
may someone help me to solve this problem.
thanks
Andreas Masur
November 23rd, 2004, 02:37 AM
[ Moved thread ]
Andreas Masur
November 23rd, 2004, 02:41 AM
Well...what type of threads do you have? Make sure that they do no destroy the handle...from MSDN:
If one of these handles is closed while the wait is still pending, the function's behavior is undefined.
cilu
November 23rd, 2004, 02:43 AM
And what is the application error that occurs?
If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError().
weiwei
November 23rd, 2004, 09:19 PM
The error is
The instruction at "0x00..." referenced memory at "0x00..."
weiwei
November 23rd, 2004, 09:22 PM
The application error occured at WaitForMultipleObjects, so i got no chance go to if statement.
NigelQ
November 24th, 2004, 12:42 AM
This sounds very much like a problem with one of your thread handles; possibly not being initialized, not valid or even destroyed (as Andreas suggested).
You can find out which one(s) by reducing the nCount value from 9 towards 1 in your WaitForMultipleObjects statement. When it begins working, you'll know that the handle at array location hEndThread[nCount] (0-based) is causing problems.
Hope this helps,
- Nigel
weiwei
November 24th, 2004, 03:33 AM
Actually i don't understand how to destroy thread handle and when it happen.
What i did is put a return 0 statement at every thread virtual Run() funtion and make sure the Run() function return 0 to terminate the thread.
Then i only put the WaitForMultipleObject to end my thread.
Is this the correct way to do it ?
Andreas Masur
November 24th, 2004, 03:42 AM
The error is
The instruction at "0x00..." referenced memory at "0x00..."
Run your application in the debugger...if the crash occur, press the 'Retry' button and you will be brought back to the debugger.
It will point to the line which caused the assertion. Open the call stack combobox and scroll down until you find the first function that you have written. Click on the line and the corresponding line/file will be displayed in the editor. Then check the variables involved to see whether everything is initialized and set correctly etc.
If this happens only with the release version...you might want to take a look at the following two articles...
Surviving the Release Version (http://www.codeproject.com/debug/survivereleasever.asp)
Finding crash information using the MAP file (http://www.codeproject.com/debug/mapfile.asp)
weiwei
November 24th, 2004, 04:38 AM
i know which line cause this error, but i don't know how to solve it.
WaitForMultipleObject statement would cause this application error
Andreas Masur
November 24th, 2004, 05:07 AM
Well...then simply debug your application...and take a look at the handles thorughout the running time...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.