PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > out of scope pointers in threads
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
out of scope pointers in threads

Réponse
 
LinkBack Outils de la discussion
Vieux 28/11/2008, 19h51   #1
uche
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut out of scope pointers in threads

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


}
  Réponse avec citation
Vieux 28/11/2008, 19h56   #2
uche
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: out of scope pointers in threads

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!
  Réponse avec citation
Vieux 28/11/2008, 20h47   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: out of scope pointers in threads

uche 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 ?


What is "data"? Is it a data type, or a pointer to a data type?
You seem to use it both ways.

> 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;


OK, we have 'data' as a data type, and 'ptr' is, I suppose a
data*.

> ptr->character = char_array;


Again, what is char_array? Where does it come from?

> 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


Anytime you need a reinterpret_cast for a function argument, you
should be asking yourself questions. If I understand the
interface description of CreateThread at MSDN (Microsoft seems
to go in a lot for obfuscated typenames), you don't need any
cast at all; just pass the pointer. (I'm guessing here that
LPVOID is a void*; where the L comes from, I don't know.)

> cout<<ptr->id<<endl;
> return 0;
> }


> DWORD WINAPI mywriteTh(data ptr)


And according to the documentation, this function must take a
void* (well, an LPVOID) as well, not a data. (The documentation
shows some __in as well. More obfuscation; I don't think it
means anything.)

> {
> //global_char is available here


> DWORD tId = GetCurrentThreadId();


> data *ptr_data = reinterpret_cast<data *>(ptr); // pointer is goes
> out of scope


Again, a reinterpret_cast is a no-no. You need a static_cast of
the void* to the exact type of the pointer that was converted to
void*, above.

> }


As for "going out of scope", of course the pointer goes out of
scope. But you're passing a copy of it to the thread, and the
memory it points to is still there.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 28/11/2008, 22h11   #4
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: out of scope pointers in threads

On 2008-11-28 14:47:04 -0500, James Kanze <james.kanze@gmail.com> said:

> (I'm guessing here that
> LPVOID is a void*; where the L comes from, I don't know.)


The L stands for LONG. It's from the days when Windows still exposed
the Intel segmented architecture, so there were SHORT and LONG variants
of pointers.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Vieux 28/11/2008, 23h25   #5
Maic Schmidt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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)Window Thread,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!



  Réponse avec citation
Vieux 29/11/2008, 11h17   #6
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: out of scope pointers in threads

On Nov 28, 10:11pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-11-28 14:47:04 -0500, James Kanze <james.ka...@gmail.com> said:


> > (I'm guessing here that
> > LPVOID is a void*; where the L comes from, I don't know.)


> The L stands for LONG. It's from the days when Windows still
> exposed the Intel segmented architecture, so there were SHORT
> and LONG variants of pointers.


Interesting example of why you shouldn't use Hungarian
notation:-).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 30/11/2008, 12h53   #7
Hendrik Schober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: out of scope pointers in threads

James Kanze wrote:
>
> [LPVOID]
>
> Interesting example of why you shouldn't use Hungarian
> notation:-).


But this isn't HN. ('lpvMyPtr' would be.)

Schobi
  Réponse avec citation
Vieux 30/11/2008, 15h46   #8
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: out of scope pointers in threads

On 2008-11-30 06:53:48 -0500, Hendrik Schober <spamtrap@gmx.de> said:

> James Kanze wrote:
>>
>> [LPVOID]
>>
>> Interesting example of why you shouldn't use Hungarian
>> notation:-).

>
> But this isn't HN. ('lpvMyPtr' would be.)
>


Well, yes, but Microsoft certainly promoted it as Hungarian Notation,
despite Charles Simonyi's disclaimers.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Vieux 30/11/2008, 19h18   #9
Hendrik Schober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: out of scope pointers in threads

Pete Becker wrote:
> On 2008-11-30 06:53:48 -0500, Hendrik Schober <spamtrap@gmx.de> said:
>
>> James Kanze wrote:
>>> [LPVOID]
>>>
>>> Interesting example of why you shouldn't use Hungarian
>>> notation:-).

>> But this isn't HN. ('lpvMyPtr' would be.)
>>

>
> Well, yes, but Microsoft certainly promoted it as Hungarian Notation,
> despite Charles Simonyi's disclaimers.


I thought I knew the difference between Charles' idea (i.e
prefixing with meaning) and its application at MS (prefixing
with type). I hadn't thought that HN -- in the MS sense of
the word -- applied to types.

Schobi
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h27.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 4,04137 seconds with 17 queries