Click here to Skip to main content
15,919,245 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: File System Methods Pin
Tom Moore21-Sep-06 11:29
Tom Moore21-Sep-06 11:29 
QuestionRe: File System Methods Pin
David Crow22-Sep-06 3:23
David Crow22-Sep-06 3:23 
AnswerRe: File System Methods Pin
Tom Moore23-Sep-06 10:46
Tom Moore23-Sep-06 10:46 
GeneralRe: File System Methods Pin
David Crow25-Sep-06 2:56
David Crow25-Sep-06 2:56 
Questionhow to convert from WPARAM or LPARAM to CString Pin
singersinger20-Sep-06 2:02
singersinger20-Sep-06 2:02 
AnswerRe: how to convert from WPARAM or LPARAM to CString Pin
Naveen20-Sep-06 2:17
Naveen20-Sep-06 2:17 
GeneralRe: how to convert from WPARAM or LPARAM to CString Pin
singersinger20-Sep-06 2:36
singersinger20-Sep-06 2:36 
AnswerRe: how to convert from WPARAM or LPARAM to CString Pin
Jörgen Sigvardsson20-Sep-06 2:31
Jörgen Sigvardsson20-Sep-06 2:31 
If you post a message, you can never be sure when in time the message will arrive on the other side. As such, you cannot use a CString object, unless you know for sure that will be alive when it arrives at the message's recipient. Consider this:
{
   CString str = ...;
   PostThreadMessage(m_pThread->m_nThreadID, WM_MSG, 0, LPARAM(str.GetBuffer());
}
// By now, the str object is dead (destructor has been called)

// Recipient function
LRESULT Handler(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
   LPCTSTR lpsz = LPCTSTR(lParam);
   DoStuff(lpsz);
}
Can you see the race condition? This will work, if and only if, the Handler() function gets to execute before the str goes out of scope (by which time it's destructor is called). Chances are (very likely) that the str object has been destroyed before Handler() is called. You passed a pointer to the internal string buffer inside the str object. That internal string buffer is deleted in the destructor of the str object. So, Handler() will receive a dangling pointer...

It is much safer if you do this:
{
   CString str = ...;
   TCHAR* copy = new TCHAR[str.GetLength() + 1];
   lstrcpy(copy, str.GetBuffer()); str.ReleaseBuffer();
   PostThreadMessage(m_pThread->m_nThreadID, WM_MSG, 0, LPARAM(copy));
}

// Recipient function
LRESULT Handler(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
   LPTSTR lpsz = LPCTSTR(lParam);
   DoStuff(lpsz);
   delete [] lpsz;
}
You allocate a copy of the string in the which you pass to the handler. It is then up to the handler to delete the string.


--
Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!

GeneralRe: how to convert from WPARAM or LPARAM to CString Pin
Zac Howland20-Sep-06 3:36
Zac Howland20-Sep-06 3:36 
AnswerRe: how to convert from WPARAM or LPARAM to CString Pin
prasad_som20-Sep-06 2:31
prasad_som20-Sep-06 2:31 
GeneralRe: how to convert from WPARAM or LPARAM to CString Pin
Jörgen Sigvardsson20-Sep-06 3:34
Jörgen Sigvardsson20-Sep-06 3:34 
Questioncreating an array of objects dynamically Pin
erfi20-Sep-06 1:56
erfi20-Sep-06 1:56 
AnswerRe: creating an array of objects dynamically Pin
toxcct20-Sep-06 1:57
toxcct20-Sep-06 1:57 
GeneralRe: creating an array of objects dynamically Pin
erfi20-Sep-06 1:59
erfi20-Sep-06 1:59 
GeneralRe: creating an array of objects dynamically Pin
toxcct20-Sep-06 2:06
toxcct20-Sep-06 2:06 
GeneralRe: creating an array of objects dynamically Pin
erfi20-Sep-06 2:09
erfi20-Sep-06 2:09 
GeneralRe: creating an array of objects dynamically Pin
Zac Howland20-Sep-06 3:46
Zac Howland20-Sep-06 3:46 
AnswerRe: creating an array of objects dynamically Pin
_AnsHUMAN_ 20-Sep-06 2:14
_AnsHUMAN_ 20-Sep-06 2:14 
GeneralRe: creating an array of objects dynamically Pin
erfi20-Sep-06 2:55
erfi20-Sep-06 2:55 
GeneralRe: creating an array of objects dynamically Pin
benjymous20-Sep-06 3:25
benjymous20-Sep-06 3:25 
GeneralRe: creating an array of objects dynamically [modified] Pin
_AnsHUMAN_ 20-Sep-06 3:33
_AnsHUMAN_ 20-Sep-06 3:33 
GeneralRe: creating an array of objects dynamically Pin
toxcct20-Sep-06 4:35
toxcct20-Sep-06 4:35 
QuestionCFile Pin
Sonia Horra20-Sep-06 1:39
Sonia Horra20-Sep-06 1:39 
AnswerRe: CFile Pin
_AnsHUMAN_ 20-Sep-06 1:42
_AnsHUMAN_ 20-Sep-06 1:42 
GeneralRe: CFile Pin
Sonia Horra20-Sep-06 2:52
Sonia Horra20-Sep-06 2:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.