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]
# 1 Re: Fail to end thread
[ Moved thread ]
Andreas Masur at 2007-11-9 13:56:50 >
# 2 Re: Fail to end thread
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.
Andreas Masur at 2007-11-9 13:57:51 >
# 3 Re: Fail to end thread
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().
cilu at 2007-11-9 13:58:50 >
# 4 Re: Fail to end thread
The error is
The instruction at "0x00..." referenced memory at "0x00..."
weiwei at 2007-11-9 13:59:55 >
# 5 Re: Fail to end thread
The application error occured at WaitForMultipleObjects, so i got no chance go to if statement.
weiwei at 2007-11-9 14:00:55 >
# 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)
Andreas Masur at 2007-11-9 14:04:02 >
# 9 Re: Fail to end thread
i know which line cause this error, but i don't know how to solve it.

WaitForMultipleObject statement would cause this application error
weiwei at 2007-11-9 14:05:02 >
# 10 Re: Fail to end thread
Well...then simply debug your application...and take a look at the handles thorughout the running time...
Andreas Masur at 2007-11-9 14:06:03 >