Threads not terminating in Win7-MATLAB using _endthreadex
===

i have a problem in a program i've written in MSVS10 using C++. I've created a dll (actually a MATLAB mexw32 shared lib) which created 4 worker threads (+1 main) and after completing the work, produces a segmentation fault in MATLAB. When i (compile) and run the code in WinXP, it runs fine. But when i do it in Win7-32bit (with exactly the same code), MATLAB crashes. Sample code is below.
  
    #include ... //includes here
    #define MAX_THREADS 4
    #define TOTAL_THREADS (MAX_THREADS+1)

    //static declarations here. Threading vars are below
    static int workerArgs[MAX_THREADS];
    static unsigned int  dwThreadIdArray[MAX_THREADS];
    static HANDLE  hThreadArray[MAX_THREADS]; 
    static int checkPoint1=TOTAL_THREADS;
    static int checkPoint2=TOTAL_THREADS;
    static int checkPoint3=TOTAL_THREADS;
    static int exitFlag=0;
    static CRITICAL_SECTION  cp1MUTEX;
    static CRITICAL_SECTION  cp2MUTEX;


    void cleanup(void) {
	//do some cleanup, delete vars etc
     }


    //worker thread function
    unsigned __stdcall workerFunc( void* threadNum )
    {
	int wTnum=*((int*)threadNum)+1;
        while(1){
	 //do stuff
	 EnterCriticalSection(&cp2MUTEX);
	 checkPoint2--;
	 LeaveCriticalSection(&cp2MUTEX);
	 if(exitFlag==1){break;}
	 else{SuspendThread(GetCurrentThread());}
	
	}
        _endthreadex( 0 );
        return 0;
    } 

    
    //main function of the dll. This is called from MATLAB
    void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){

	//other var declarations here

	//Initialize vars
	if (!initialized) {

		//and yet more..

		//THREADING init code
		//some init for the vars used in the threads here

		//create thread number array
		for(int i=0; i<MAX_THREADS; i++ ){workerArgs[i]=i;}
		//create mutexes
		InitializeCriticalSection(&cp1MUTEX);
		InitializeCriticalSection(&cp2MUTEX);

		exitFlag=(int)mxGetScalar(prhs[18]); //get exitflag value from input

		// create worker threads
		for(int i=0; i<MAX_THREADS; i++ ){
			hThreadArray[i] = (HANDLE)_beginthreadex(NULL,0,&workerFunc,
				(void*)(workerArgs +i),CREATE_SUSPENDED ,&dwThreadIdArray[i]);}


		mexAtExit(cleanup);  //function to call upon completion
		initialized=1;

	}
	


	//do stuff

	exitFlag=(int)mxGetScalar(prhs[18]);  //dll checks for exitflag to go 1 to kill threads and exit

	//do stuff
	
	//resume threads
	for(int i=0; i<MAX_THREADS; i++){ResumeThread(hThreadArray[i]);}


	//do stuff

	EnterCriticalSection(&cp2MUTEX);
	checkPoint2--;
	LeaveCriticalSection(&cp2MUTEX);
	
	
	while(checkPoint2!=0){}  //wait for threads to finish
	
	//do stuff
	
        //kill all threads and exit
	if(exitFlag==1){  
		WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);
		for(int i=0; i<MAX_THREADS; i++){
			CloseHandle(hThreadArray[i]);
			}
		DeleteCriticalSection(&cp1MUTEX);
		DeleteCriticalSection(&cp2MUTEX);
	}

	return;
    }

So how does this work? I call the dll from matlab with exitflag=0, thus initializing all the vars and creating the threads. Then i loop inside matlab calling the dll. When done, i call the dll with exitflag=1. This deletes all threads and terminates the main function (which calls the cleanup function). In WinxP this works like a charm. In Win7 (with the same MATLAB version and the same code) it works without problems for the first "run" (e.g. call the dll 100 times and then call it with exitflag=1). If i try to re-run the code or clear the memory ("clear all" in matlab), matlab produces a segmentation fault.
Any ideas?
