out of scope pointers in threads

Discussion in 'C++' started by uche, Nov 28, 2008.

  1. uche

    uche Guest

    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


    }
    uche, Nov 28, 2008
    #1
    1. Advertising

  2. uche

    uche Guest

    On Nov 28, 10:51 am, 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 ?
    >
    > 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!
    uche, Nov 28, 2008
    #2
    1. Advertising

  3. uche

    James Kanze Guest

    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:
    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
    James Kanze, Nov 28, 2008
    #3
  4. uche

    Maic Schmidt Guest

    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" <> schrieb im Newsbeitrag
    news:...
    > On Nov 28, 10:51 am, 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 ?
    > >
    > > 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!
    Maic Schmidt, Nov 28, 2008
    #4
  5. uche

    James Kanze Guest

    On Nov 28, 10:11 pm, Pete Becker <> wrote:
    > On 2008-11-28 14:47:04 -0500, James Kanze <> 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:
    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
    James Kanze, Nov 29, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    926
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    647
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    458
  4. Angus
    Replies:
    1
    Views:
    277
    Maxim Yegorushkin
    Nov 19, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    652
Loading...

Share This Page