Re: AfxBeginThread argument is altered
From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 02/13/05
- Next message: Joseph M. Newcomer: "Re: Serialize puzzle"
- Previous message: Joseph M. Newcomer: "Re: WM_KILLFOCUS doesn´t appear in PreTranslateMessage"
- In reply to: Joseph V: "AfxBeginThread argument is altered"
- Next in thread: Joseph V: "Re: AfxBeginThread argument is altered"
- Reply: Joseph V: "Re: AfxBeginThread argument is altered"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 12 Feb 2005 21:01:14 -0500
Pretty screamingly obvious what has happened here. You passed in a pointer to a
stack-allocated variable. By the time the thread executes, the stack has gone away and
whatever was on it is gone. You must never pass in a pointer to a stack local unless you
can absolutely guarantee that the stack will remain intact until the thread terminates.
This is usually very tricky, and always involves halting the intiating thread, thus
defeating the purpose of having a separate thread. Simply allocate the arguments on the
heap and make sure the thread frees them before it terminates.
void * args = new void *[2];
args[0] = ...;
args[1] = ...
by the way, putting a void * cast is pretty pointless, because anything is implicitly cast
to void *.
After you have taken the values from the args list, you can do a delete [] on it. Read my
essay on worker threads on my MVP Tips site.
joe
On Fri, 11 Feb 2005 08:52:48 -0600, "Joseph V" <jvannucc.nospam@cullme.memphis.edu> wrote:
>First time I've posted, thx in advance, this group has helped me smash many
>bugs already!
>
>My problem is that I'm spawning a worker thread to run a genetic algorithm
>as
>to not deadlock my main dialog for the usually lengthy time is takes to run
>a GA.
>About 80% of the time it runs fine, and under a debug build it runs
>flawlessly every time.
>
>But if I run under my release build it will occasionally (maybe 20% of the
>time) not get the
> proper arguments (a void pointer to an array of two casted void pointers to
>my window
>handler and my dialog box). An example output of my Afx boxes on fail are:
>0012FC38 0012FC18
>before the thread and: 77D487FF 0012F5F4 inside the thread. On success they
>are
>the same.
>
>Needless to say my messages aren't processed by the win32 message handler
>and the data update
>functions are never being called by the GA (to update the GUI).
>
>Please help! Code follows,
>
>Best Regards,
>Joseph V
>
>
>
>void* args[2];
>args[0] = static_cast<void*>(&m_hWnd); //<-window handler
>args[1] = static_cast<void*>(this); //dialog gui
>ostringstream mystring;
>mystring << args[0] << " " << args[1] << endl;
>AfxMessageBox(mystring.str().c_str());
>m_thread = AfxBeginThread(runGA, args);
>------------------------------------------------
>int runGA(void* arg) {
>void** args = static_cast<void**>(arg);
>HWND* hwnd = static_cast<HWND*>(args[0]); //<-window
>handler
>CMYDlg* window = static_cast<CMYDlg*>(args[1]); //dialog gui
>ostringstream debugme;
>debugme << (void*) hwnd << " " << (void*) window << endl;
>AfxMessageBox(debugme.str().c_str());
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
- Next message: Joseph M. Newcomer: "Re: Serialize puzzle"
- Previous message: Joseph M. Newcomer: "Re: WM_KILLFOCUS doesn´t appear in PreTranslateMessage"
- In reply to: Joseph V: "AfxBeginThread argument is altered"
- Next in thread: Joseph V: "Re: AfxBeginThread argument is altered"
- Reply: Joseph V: "Re: AfxBeginThread argument is altered"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|