Multithread heap assertion failure(Continued)



I create a win32 console project with mfc support(shared dll) using
VS2003, and add the following source file to my project.

When run, there is "debug assertion failure" error around here in
thrdcore.cpp:
--------------
// check to make sure the VTable pointer is valid
ASSERT(sizeof(CObject) == sizeof(void*));
if (!AfxIsValidAddress(*(void**)pOb, sizeof(void*), FALSE))
{
TRACE(traceAppMsg, 0, "ASSERT_VALID fails with illegal vtable
pointer.\n");
if (AfxAssertFailedLine(lpszFileName, nLine))
AfxDebugBreak();
return; // quick escape
}
--------------
The exact position is at "AfxDebugBreak()" in the deepest place.


Having programmed for years, I am still new to multithreaded
programming.
I would usually refer to docs when I have something to check.
But, as for multithread programming, I don't know what is going
wrong.
And I don't know which part of mannual should I read.
I have to ask you for help.

*Any help will be appreciated. Thanks in advance.*

Here is the call stack:
---------------
mfc71d.dll!AfxAssertValidObject(const CObject * pOb=0x00a555f4, const
char * lpszFileName=0x7c15264c, int nLine=375) 行92 C++
mfc71d.dll!AfxEndThread(unsigned int nExitCode=1, int bDelete=1) 行
376 C++
mfc71d.dll!_AfxThreadEntry(void * pParam=0x0013fd98) 行134 C++
msvcr71d.dll!_threadstartex(void * ptd=0x00a55810) 行241 + 0xd C
kernel32.dll!7c80b683()


And Here is the source file:
---------------
#include <afx.h>
#include <afxmt.h>
#include <afxwin.h> // MFC core and standard components
#include <windows.h>

#include <iostream>
using namespace std;

class CTestThread: public CWinThread{
public:
long timeout;

CTestThread(){}
virtual ~CTestThread(){}

int Run(){
cout << "run..." << endl;
Sleep(timeout);
return 1;
}

BOOL InitInstance(){
cout << "init..." << endl;
return TRUE;
}

int ExitInstance(){
cout << "exit inst..." << endl;
return 0;
}
};

int main(){
int nWorkerNum = 2;
CTestThread * m_pThread = new CTestThread[nWorkerNum];
HANDLE * m_pThreadHandle = new HANDLE[nWorkerNum];
for(int i = 0; i < nWorkerNum; i ++){
m_pThread[i].CreateThread();
m_pThread[i].m_bAutoDelete = FALSE;
m_pThreadHandle[i] = m_pThread[i].m_hThread;
}
m_pThread[0].timeout = 1;
m_pThread[1].timeout = 3000;
WaitForMultipleObjects(nWorkerNum, m_pThreadHandle, FALSE, INFINITE);
cout << "here" << endl; cout.flush();
clock_t s = clock();
WaitForMultipleObjects(nWorkerNum, m_pThreadHandle, TRUE, 2500);
clock_t e = clock();
cout << "####" << (e - s) << endl;
cout << "there" << endl; cout.flush();

for(int i = 0; i < nWorkerNum; i ++){
m_pThread[i].ExitInstance();
}

delete [] m_pThreadHandle;
delete [] m_pThread;

cout << "Press c to conitnue..." << endl;
cout.flush();
char c; cin >> c;
}

.