Fail to end thread
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
[1144 byte] By [
weiwei] at [2007-11-19 2:50:56]

# 6 Re: Fail to end thread
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
NigelQ at 2007-11-9 14:01:54 >

# 7 Re: Fail to end thread
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 ?
weiwei at 2007-11-9 14:02:58 >

# 8 Re: Fail to end thread
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)