Click here to Skip to main content
15,913,115 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How can I EDIT/DELETE Excel-records Pin
Tim Deveaux10-Oct-00 5:05
Tim Deveaux10-Oct-00 5:05 
AnswerRe: How can I EDIT/DELETE Excel-records Pin
Erik Funkenbusch10-Oct-00 11:20
Erik Funkenbusch10-Oct-00 11:20 
QuestionThread & Message pb? Pin
Ori10-Oct-00 2:15
Ori10-Oct-00 2:15 
AnswerRe: Thread & Message pb? Pin
Ori10-Oct-00 2:45
Ori10-Oct-00 2:45 
AnswerRe: Thread & Message pb? Pin
Erik Funkenbusch10-Oct-00 11:22
Erik Funkenbusch10-Oct-00 11:22 
GeneralRe: Thread & Message pb? Pin
Ori11-Oct-00 1:50
Ori11-Oct-00 1:50 
GeneralRe: Thread & Message pb? Pin
Erik Funkenbusch11-Oct-00 10:15
Erik Funkenbusch11-Oct-00 10:15 
AnswerRe: Thread & Message pb? Pin
Mark Jones18-Oct-00 11:25
Mark Jones18-Oct-00 11:25 
Hi Ori,

I agree with the other poster who mentions that your user message definitions should start at WM_APP, rather than WM_USER. I actually start at WM_APP+0x100 just to be sure of no clash. However, I do not think this is the problem, and I don't think we can put the problem down to differences between win 9x and win nt.

The problem you described can be caused if the destruction of the sString object is not *guaranteed* to be determined by the code receiving MY_MSG in the main program. For example, allocating sString on the stack in a worker thread would be bad news. This is because you don't know if sString has been destructed when the main program gets round to pumping its message queue sufficiently to get hold of MY_MSG. On some program runs it might work - others it won't, such is the realm of multi-threaded programming.

This can be avoided by using SendMessage, which will block your worker thread until the main program has processed MY_MSG, thus guaranteeing sString points to valid memory for the duration of execution of the MY_MSG handler.

I'll give an example of how I'd do this using PostMessage. If I've got the wrong end of the stick - sorry - please post me and I'll try again - this is my first post to CodeProject.

void CWorkerThread::SomeFunction()
{
CString* pstrText=new CString(_T("Test"));
int iSomeNumber=0;

// Transfer ownership of the memory pointed to by
// pstrText to the receiver of MY_MSG, ie he who
// processes MY_MSG must do 'delete pstrText'.

::PostMessage(MY_MSG, (WPARAM)pstrText, (LPARAM)iSomeNumber);

// This thread does not control the lifetime of the
// memory pointer to by pstrText now. So...
// we mustn't delete pstrText here
// we shouldn't dereference pstrText here
// and probably should just deny all knowledge
// that *pstrText ever existed... nullify pstrText!

pstrText=NULL;
}

LRESULT CMainProgram::OnMyMsg(WPARAM wParam, LPARAM lParam)
{
// Document the fact that the memory pointed to by
// pstrText is now 'owned' by this function.

CString* pstrText=(CString*)wParam;
int iSomeNumber=(int)lParam;

// do some stuff with pstrText and iSomeNumber
// ....

// because we own pstrText, we must delete it
delete pstrText;
pstrText=NULL; // just for safety

return 0;
}

Does this help?


Mark
QuestionCan anyone help me draw to a picture control? Pin
Rick Gungadoo10-Oct-00 1:15
sussRick Gungadoo10-Oct-00 1:15 
Questionhow to change the font for a label Pin
Mac9-Oct-00 22:10
Mac9-Oct-00 22:10 
AnswerRe: how to change the font for a label Pin
frydaysoft.de10-Oct-00 7:25
sussfrydaysoft.de10-Oct-00 7:25 
QuestionStrings from a text file or database? Pin
Fredrik9-Oct-00 21:40
Fredrik9-Oct-00 21:40 
GeneralTitleBar Text Pin
Frank Deo9-Oct-00 11:14
Frank Deo9-Oct-00 11:14 
GeneralMS Access 2000 and VC6 Pin
Gozer9-Oct-00 9:48
Gozer9-Oct-00 9:48 
GeneralWin32API/GDI/Fonts Pin
Kirill Sherman9-Oct-00 9:29
sussKirill Sherman9-Oct-00 9:29 
GeneralCopy constructor in Exception Class Pin
Robin8-Oct-00 16:41
Robin8-Oct-00 16:41 
GeneralRe: Copy constructor in Exception Class Pin
Tim Deveaux9-Oct-00 5:08
Tim Deveaux9-Oct-00 5:08 
GeneralSingle Document naming Pin
kin8-Oct-00 13:28
kin8-Oct-00 13:28 
GeneralRe: Single Document naming Pin
Tim Deveaux8-Oct-00 14:48
Tim Deveaux8-Oct-00 14:48 
GeneralRe: Single Document naming Pin
Sam Hobbs9-Oct-00 20:27
Sam Hobbs9-Oct-00 20:27 
GeneralEnabling/graying normal menu items? (Win 2000) Pin
Martin Vrbovsky8-Oct-00 12:09
sussMartin Vrbovsky8-Oct-00 12:09 
GeneralRe: Enabling/graying normal menu items? (Win 2000) Pin
Michael Dunn9-Oct-00 9:58
sitebuilderMichael Dunn9-Oct-00 9:58 
General*** Displaying TABLES from Access in a MFC application *** Pin
Steve Lai8-Oct-00 8:53
Steve Lai8-Oct-00 8:53 
General*** Saving TABLES from Access with MFC application *** Pin
Steve Lai8-Oct-00 8:52
Steve Lai8-Oct-00 8:52 
Generaldocking Pin
mike99998-Oct-00 6:53
mike99998-Oct-00 6:53 

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.