Threads and memory in MFC
Posted on 2002-06-12
I have a standard queue and a flush queue thread. I have a CConnection object, and a CMessage object.
The flush queue thread pops the queue and then creates a SendMessageThread.
static void SendMessageThread(void* p)
SEND_PARAMS pSp = (SEND_PARAMS*) p;
CConnection* pConn = pSp->pConnection;
CMessage* pMess = pSp->pMessage;
// I would do send the message here
// BUT, pMessage is an points to garbage memory
// delete pMess; (throws an exception)
// delete pSp; (throws an exception)
static void FlushQueue(void* p)
message = queue.top();
SEND_PARAMS* pSp = new SEND_PARAMS;
pSp->pConnection = conn;
pSp->pMessage = new CMessage("stuff");
HANDLE hThread = (HANDLE) _beginthread(SendMessageThread, 0, (void*) &pSp);
The problem is that by the time the SendMessageThread function runs, the memory has already be de-allocated by the program. This behavior is only happening when using MFC. In a normal Win32 app (non MFC) this does not happen, ie, the memory allocated in the loop for the SEND_PARAMS is not deleted until I puposefully delete it in the SendMessageThread.
So I can tell MFC has built in garbage collection. But I need to get these params into the thread on the fly. I don't want that memory deleted until I say so. At this point taking MFC out of the app is not possible. Is there any way to get the memory to 'stick'?
If you need more clarification, let me know. Thanks, Chase707