Re: out of scope pointers in threads
Your pointer seems to go "out of scope" because your procedure where you
declared the pointer ends,
and so its local variable stack is cleared.
Declare your data-pointer at class or global scope.
class SVDaemon
{
public:
void StartThread()
{
pointer = new MyData();
wndThread =
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WindowThread,pointer,0,&threadId
);
}
private:
MyData* pointer;
DWORD threadId;
HANDLE wndThread;
static DWORD WindowThread(MyData* data);
};
Greetz
Maic
"uche" <uraniumore235@hotmail.com> schrieb im Newsbeitrag
news:c201d729-47c7-449f-9d11-62ac37d10255@a29g2000pra.googlegroups.com...
On Nov 28, 10:51 am, uche <uraniumore...@hotmail.com> wrote:
I am trying to send a pointer to the thread below; however, when the
thread gets executed, the pointer goes out of scope . How do I fix
it ?
int mywrite(char* id, int number_of_characters, char char_array)
{
HANDLE Producer;
DWORD ThId;
//global_char = char_array;
//create mutual exlusion for producer process to write into the
buffer
//create producer thread and start the function for inserting
characters
//mywriteTh is the entry point of the producer
ptr =new data;
ptr->character = char_array;
cout<<ptr->character<<endl;
ptr->id = id;
cout<<ptr->id<<endl;
Producer = (HANDLE) CreateThread (NULL, 0, mywriteTh,
reinterpret_cast<data> (ptr) , 0, &ThId); // i want to send the
pointer to this thread
cout<<ptr->id<<endl;
return 0;
}
DWORD WINAPI mywriteTh(data ptr)
{
//global_char is available here
DWORD tId = GetCurrentThreadId();
data *ptr_data = reinterpret_cast<data *>(ptr); // pointer is goes
out of scope
}
please note: change data* to LPVOID ... HOWEVER, THIS DOESN'T SEEM TO
DO THE TRICK! I STILL GET A POINTER THAT IS OUT OF SCOPE!