Click to See Complete Forum and Search --> : PostThreadMesagen CWinThread


hmaturana
April 11th, 2004, 09:23 PM
CSimple is a class derivated from CWinThread
two threads are started in the process

pThread1 = AfxBeginThread(RUNTIME_CLASS(CSimple));
pThread2 = AfxBeginThread(RUNTIME_CLASS(CSimple));

Then a PostThreadMessage is sent to OnMessageServer
the value pMsg->m_iMessage inside OnMessageServer is 22
in the second PostThreadMessage the value of pMsg->iMessage
is 0.


long CSimple::OnMessageServer(UINT wParam, LPARAM lparam)
{
CMsg* pMsg = (CMsg*) lparam;
int iMessage = pMsg->m_iMessage;
TRACE("MessageReceived");
return 0;
}



{
CMsg* pMsg = new CMsg;;
pMsg->m_iMessage = 22;

pThread1->PostThreadMessage(WM_MESSAGE_SERVER, 0, (LPARAM)pMsg);
pMsg->m_iMessage = 11;
pThread2->PostThreadMessage(WM_MESSAGE_SERVER, 0, (LPARAM)pMsg);

delete(pMsg);
}


I do not know what the problem could be so any help will be apreciated.

Thank you

Ejaz
April 11th, 2004, 11:49 PM
I don't fully understand what you are asking but what I see is...




CMsg* pMsg = new CMsg;;
pMsg->m_iMessage = 22;

pThread1->PostThreadMessage(WM_MESSAGE_SERVER, 0, (LPARAM)pMsg);
pMsg->m_iMessage = 11;
pThread2->PostThreadMessage(WM_MESSAGE_SERVER, 0, (LPARAM)pMsg);

delete(pMsg);



U see delete(pMsg); will delete the pointer right after that and PostThreadMessage is not like SendMessage, it will just send the message and will move on, and at the very next line, it will delete ur pointer.

How about if u delete the pointer, where u r receiveing WM_MESSAGE_SERVER.

btw.... don't just put 0 in case of WPARAM, it might crash ur release version, type cast it into WPARAM as well.

Andreas Masur
April 12th, 2004, 06:57 AM
[Moved thread]