Click to See Complete Forum and Search --> : Problem at the end of thread?
dtdai78
August 31st, 2005, 11:40 PM
Hello all,
I have a class like this:
class ADialog{
public:
//.....
bool CheckValid();
static void ThrProg(LPVOID pParam);
void CheckData();
//....
};
bool ADialog::CheckValid()
{
//....
AfxBeginThread((AFX_THREADPROC)ThrProg, (LPVOID)this);
//....
}
void ADialog::ThrProg(LPVOID pParam)
{
ADialog *pThr = (ADialog*)pParam;
pThr->CheckData();
return;
}
My problem is after CheckData was executed successfully, the program has an error message, say that "the apllication has encountered a problem and needs to be close. Send Error report or don't to Microsoft...."
Pls help me,
Thnk u very much.
Boris K K
September 1st, 2005, 04:22 AM
Are you sure that the ADialog instance is not destroyed before ThrProg has finished?
Does CheckData work without problems if called directly from CheckValid?
What does CheckData do?
dtdai78
September 1st, 2005, 07:47 AM
I'm sure the ADialog instance was not destroyed before ThrProg has finished. Also, if the CheckData was called directly in CheckValid without thread, it works correctly.
So I think the cause is at ThrProg thread, but I really don't know where it is.
dtdai78
September 1st, 2005, 08:13 AM
I'm sorry, I just checked and knew that the problem is here:
//////////////// in my ADialog.cpp
using namespace std;
typedef queue<CString> STRQUEUE;
STRQUEUE strQueue;
bool ADialog::CheckValid()
{
//....
AfxBeginThread((AFX_THREADPROC)ThrProg, (LPVOID)this);
//....
}
void ADialog::ThrProg(LPVOID pParam)
{
// get data from strQueue and pop it.
CString stData = strQueue.front(); // THE PROBLEM
strQueue.pop(); // IS HERE
// do processing
ADialog *pThr = (ADialog*)pParam;
pThr->CheckData(); // IT DONE
return;
}
Andreas Masur
September 1st, 2005, 09:43 AM
As long as the functions do not access the window handle (m_hWnd) you are fine. But many functions need to go back to the specific window handle and then will cause a debug assertion (access vialotion in release mode). The reason is that the window handle maps are kept in thread local storage to ensure protection from simultaneous access from multiple threads. So...there is the possibility for example that one thread might have a mapping from a handle to a C++ object, but another thread might map the same handle to a different one.
Further information can be found in the following FAQ (http://www.codeguru.com/forum/showthread.php?t=312454)...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.