/* Count is a global variable, so it will be remembered between calls */
static int Count = 1;
void MyExit()
{
mexPrintf("MyExit() called!\n");
/* Do cleanup here ... */
return;
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexAtExit(&MyExit); /* Register MyExit() to run when MEX??function is cleared */
mexPrintf("Count=%d\n", Count);
Count++; /* Increment Count */
return;
}

/
CRemember2App theApp;
int CRemember2App::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	mexPrintf("App::ExitInstance() called!\n");
	return CWinApp::ExitInstance();
}
/

>> remember2
Count=1
>> remember2
Count=2
>> clear remember2
MyExit() called!
App::ExitInstance() called!
>>