-
Thread Parameter, delete, and ClassDestructor :: MFC
Hi.
I discovered an interesting bug in an MFC multithreaded program. I found that when you delete a pointer to the worker thread's parameter (declared using new operator), the destructor of the class that is part of the parameter is called.
-----
// This is global.
struct THREADPARAMS
{
ClassA *pCA;
}
// This code is in the primary or main thread.
THREADPARAMS *ptp = new THREADPARAMS;
// Assume that clObject is a member variable and is an instance
// of a ClassA.
ptp->pCA = &clObject;
...
CWinThread *Thread = AfxBeginThread(ThreadFunction, ptp, ...);
// Assume everything works.
// Here is ThreadFunction
UINT ThreadFunction(LPVOID *pParams)
{
THREADPARAMS *ptp = reinterpret_cast<THREADPARAMS *>(pParams);
ClassA *pClassA = ptp->pCA;
// Here is the code that causes the bug.
// I studied Jeff Prosise book on MFC.
// Prosise deleted the new struct inside the worker thread.
delete ptp;
-----
Okay. Lets get to the point. "delete ptp" calls ClassA destructor. I am not sure how to get this working.
My goal is to pass into a worker thread pointers to private data members of the primary thread (main) including primitive data type and ADT. I would like to know is the situation above normal or is there a flaw in my design?
Thanks,
Kuphryn
Popular pages
Recent additions