Create Thread and parameter distortion

A

AMS

I am trying to create a thread to execute a script for me and then
display the output in notepad.

What is happening is when I call the CreateThread method and pass in
the Routine and Parameter the parameter that is being passed is
distorted when it reaches the routine. If I output the parameter in
the routine it is just garbage. It works fine on Win NT but not in Win
2000.

Here is my method that calls CreateThread

void CTestDlg::BeginScriptOnNewThread()
{
DWORD threadID;
char commandline[1000];

strcpy(commandline, m_commandline);

HANDLE ThreadHandle=CreateThread(0,0,(LPTHREAD_START_ROUTINE)StartScript,
&commandline,
0,&threadID);
return;
}

Any ideas?

Thanks!
 
V

Victor Bazarov

AMS said:
I am trying to create a thread to execute a script for me and then
display the output in notepad.

What is happening is when I call the CreateThread method and pass in
the Routine and Parameter the parameter that is being passed is
distorted when it reaches the routine. If I output the parameter in
the routine it is just garbage. It works fine on Win NT but not in Win
2000.

Here is my method that calls CreateThread

void CTestDlg::BeginScriptOnNewThread()
{
DWORD threadID;
char commandline[1000];

strcpy(commandline, m_commandline);

HANDLE ThreadHandle=CreateThread(0,0,(LPTHREAD_START_ROUTINE)StartScript,
&commandline,
0,&threadID);
return;
}

Any ideas?

'commandline' is a local variable. By the time a new thread is
created the memory occupied by 'commandline' is either GONE
because the function 'BeginScriptOnNewThread' has finished and
all local variables have ceased to exist, or because your thread
has different stack (the former is more likely) and 'commandline'
pointer is not a valid pointer or points to something else there.

You have to copy 'commandline' into a global variable so that
the thread will be able to access it.

Victor
 
H

Howard Hinnant

| > I am trying to create a thread to execute a script for me and then
| > display the output in notepad.
| >
| > What is happening is when I call the CreateThread method and pass in
| > the Routine and Parameter the parameter that is being passed is
| > distorted when it reaches the routine. If I output the parameter in
| > the routine it is just garbage. It works fine on Win NT but not in Win
| > 2000.
| >
| > Here is my method that calls CreateThread
| >
| > void CTestDlg::BeginScriptOnNewThread()
| > {
| > DWORD threadID;
| > char commandline[1000];
| >
| > strcpy(commandline, m_commandline);
| >
| > HANDLE ThreadHandle=CreateThread(0,0,(LPTHREAD_START_ROUTINE)StartScript,
| > &commandline,
| > 0,&threadID);
| > return;
| > }
| >
| > Any ideas?
|
|
| Well CreateThread can return before the thread actually gets any time...
| This means there is a possibility that commandline isn't allocated anymore
| by the time you use it in the thread (it doesnt exist anymore by the time
| you exit BeginScriptOnNewThread(). There are many ways around this, one of
| them being :
|
| void CTestDlg::BeginScriptOnNewThread()
| {
| DWORD threadID;
| char *commandline= new char[1000];
| strcpy(commandline, m_commandline);
|
| HANDLE
| ThreadHandle=CreateThread(0,0,(LPTHREAD_START_ROUTINE)StartScript,
| commandline, 0, &threadID);
|
| return;
| }
|
| *** In the thread routine, make sure you use delete on the pointer when done
| with it ***
|
|
| If the thread is garanteed to be done by the time your dialog is closed,
| then you can skip the copy altogether and use &m_commandline directly,
| assuming neither the thread or dialog modify this variable...
|
| If the param is only used to initialize the thread, you could also use
| synchronisation... You could wait for the thread to signal that it's safe to
| return, before using return in the function that creates the thread...
|
| As for why it works in NT, well I suppose it's just luck... Probably a very
| subtle difference in the scheduling or thread initialization mechanism or
| something... Thing is it can or cannot work from one execution to the
| next... But it's always wrong, even when it works, since you're using memory
| that you very probably do not own...

You might take a look at the boost thread and function libs
(www.boost.org). The combination takes care of this problem very
nicely.

You might code it up like:

struct hold_command
{
hold_command(const char* command)
{
strcpy(m_commandline, command);
}

void operator()()
{
StartScript(m_commandline);
}
private:
char m_commandline[1000];
};

void CTestDlg::BeginScriptOnNewThread()
{
thread threadID(function<void()>(hold_command(m_commandline)));
}

The boost thread starter function doesn't take any parameters. But as
you can see it is easy to create a functor that converts a function
that takes a parameter into one that doesn't. And then the thread
constructor takes care of the lifetime issues for you. It insures that
the new thread is off and running before the functor used to start it
destructs.
 
A

AMS

Alex -
Thank you so much for your response! It was much appreciated. I didn't
write the code - just have to fix it so I probably wouldn't have come
to this realization on my own - still new to C++. I ended up just
waiting for the thread to complete before returning and now it works
great! :)

Thanks again,
Abby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top