Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. static_cast and reinterpret_cast

static_cast and reinterpret_cast

Scheduled Pinned Locked Moved C / C++ / MFC
questiondiscussion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LCI
    wrote on last edited by
    #1

    In a dll, i start a thread using _beginthread like: _beginthread(Mythread,0,LPVOID(this)); void MyThread(LPVOID pVoid) { CMyClass* pClass = static_cast<CMyClass*>(pVoid); pClass->SetValue(1); } Everything works nicely until i try to access one of the member vars from CMyClass. The app begins to lock up on me. I may be missing something fundamental here. Anyone have any thoughts?

    M P E 3 Replies Last reply
    0
    • L LCI

      In a dll, i start a thread using _beginthread like: _beginthread(Mythread,0,LPVOID(this)); void MyThread(LPVOID pVoid) { CMyClass* pClass = static_cast<CMyClass*>(pVoid); pClass->SetValue(1); } Everything works nicely until i try to access one of the member vars from CMyClass. The app begins to lock up on me. I may be missing something fundamental here. Anyone have any thoughts?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      There's nothing in the code you've shown that looks like it would lock anything up. Actually the thread shown could finish before _beginthread() completes on the calling thread :) You can examine the call stack of all the threads in the debugger when it's locked up and maybe that will help determine the offending code. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • L LCI

        In a dll, i start a thread using _beginthread like: _beginthread(Mythread,0,LPVOID(this)); void MyThread(LPVOID pVoid) { CMyClass* pClass = static_cast<CMyClass*>(pVoid); pClass->SetValue(1); } Everything works nicely until i try to access one of the member vars from CMyClass. The app begins to lock up on me. I may be missing something fundamental here. Anyone have any thoughts?

        P Offline
        P Offline
        Peter Weyzen
        wrote on last edited by
        #3

        Here's my fundamental question about your thread.... You're now accessing "this" from your thread? And are you also accessing "this" from the thread that created it? Are you employing appropriate locks to protect "this"?

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

        1 Reply Last reply
        0
        • L LCI

          In a dll, i start a thread using _beginthread like: _beginthread(Mythread,0,LPVOID(this)); void MyThread(LPVOID pVoid) { CMyClass* pClass = static_cast<CMyClass*>(pVoid); pClass->SetValue(1); } Everything works nicely until i try to access one of the member vars from CMyClass. The app begins to lock up on me. I may be missing something fundamental here. Anyone have any thoughts?

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #4

          LCI wrote:

          Everything works nicely until i try to access one of the member vars from CMyClass.

          You mean, if you try to access a specific member or "ANY" member?


          OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

          L 1 Reply Last reply
          0
          • E Eytukan

            LCI wrote:

            Everything works nicely until i try to access one of the member vars from CMyClass.

            You mean, if you try to access a specific member or "ANY" member?


            OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

            L Offline
            L Offline
            LCI
            wrote on last edited by
            #5

            Any member this seems to have an issue with .

            R 1 Reply Last reply
            0
            • L LCI

              Any member this seems to have an issue with .

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              what do you mean by "The app begins to lock up on you". if you are human being i don't know whether you are using such an advanced technology :) The bad cast can endup in undefined state, can you please post what you are passing to the thread?

              L 1 Reply Last reply
              0
              • R Rajkumar R

                what do you mean by "The app begins to lock up on you". if you are human being i don't know whether you are using such an advanced technology :) The bad cast can endup in undefined state, can you please post what you are passing to the thread?

                L Offline
                L Offline
                LCI
                wrote on last edited by
                #7

                Here is what i have: The app freezes in Class B which is a dll. I know that there is something wrong with the parameter that the thread is receiving in that dll, but i cannot figure it out. ClassA { CWinThread* pThreadA[3] = {NULL}; CClassA::BeginAllThreads() { CClassC* pApp = reinterpret_cast(AfxGetApp()); if (NULL == pThreadA[i]) { pThreadA[i] = AfxBeginThread(ThreadGetOne, (LPVOID)pApp->m_arrPtr[i], THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL); pThreadA[i]->m_AutoDelete = FALSE; pThreadA[i]->ResumeThread(); pApp->arrPtr[i]->StartThread(); } } void ThreadGetOne(LPVOID pVoid) { CClassB* pPointer = (CClassB*)pVoid while (TRUE) { pPointer->Readport(); } } } //This is a .dll ClassB { void CClassB::StartThread() { _beginthread(MYBThread, 0, LPVOID(this)); } void MYBThread(void* pVoid) { CClassB* pPointer = static_cast(pVoid); while (pPointer->IsOn()) //HERE IS WHERE THE APP Freezes { //do something } _endthread(); } } ClassC { CClassB* m_arrPtr[3]; }

                R 1 Reply Last reply
                0
                • L LCI

                  Here is what i have: The app freezes in Class B which is a dll. I know that there is something wrong with the parameter that the thread is receiving in that dll, but i cannot figure it out. ClassA { CWinThread* pThreadA[3] = {NULL}; CClassA::BeginAllThreads() { CClassC* pApp = reinterpret_cast(AfxGetApp()); if (NULL == pThreadA[i]) { pThreadA[i] = AfxBeginThread(ThreadGetOne, (LPVOID)pApp->m_arrPtr[i], THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL); pThreadA[i]->m_AutoDelete = FALSE; pThreadA[i]->ResumeThread(); pApp->arrPtr[i]->StartThread(); } } void ThreadGetOne(LPVOID pVoid) { CClassB* pPointer = (CClassB*)pVoid while (TRUE) { pPointer->Readport(); } } } //This is a .dll ClassB { void CClassB::StartThread() { _beginthread(MYBThread, 0, LPVOID(this)); } void MYBThread(void* pVoid) { CClassB* pPointer = static_cast(pVoid); while (pPointer->IsOn()) //HERE IS WHERE THE APP Freezes { //do something } _endthread(); } } ClassC { CClassB* m_arrPtr[3]; }

                  R Offline
                  R Offline
                  Rajkumar R
                  wrote on last edited by
                  #8

                  LCI wrote:

                  pApp->arrPtr[i]->StartThread();

                  are you correctly assigning the Object for arrPtr[i]? as far you are not dereferencing the this pointer you can call a member function with invalid pointer. void CClassB::StartThread() { _beginthread(MYBThread, 0, LPVOID(this)); } in startThread function you are only passing the this pointer which is not deferenced, possibly IsOn is using the this pointer crashes the application.

                  L 1 Reply Last reply
                  0
                  • R Rajkumar R

                    LCI wrote:

                    pApp->arrPtr[i]->StartThread();

                    are you correctly assigning the Object for arrPtr[i]? as far you are not dereferencing the this pointer you can call a member function with invalid pointer. void CClassB::StartThread() { _beginthread(MYBThread, 0, LPVOID(this)); } in startThread function you are only passing the this pointer which is not deferenced, possibly IsOn is using the this pointer crashes the application.

                    L Offline
                    L Offline
                    LCI
                    wrote on last edited by
                    #9

                    I sort of see what you are saying. Can you expand a little? Thanks,

                    R 1 Reply Last reply
                    0
                    • L LCI

                      I sort of see what you are saying. Can you expand a little? Thanks,

                      R Offline
                      R Offline
                      Rajkumar R
                      wrote on last edited by
                      #10

                      are you sure arrPtr[i] contains valid object? debug it or show the code that fills the array.

                      L 1 Reply Last reply
                      0
                      • R Rajkumar R

                        are you sure arrPtr[i] contains valid object? debug it or show the code that fills the array.

                        L Offline
                        L Offline
                        LCI
                        wrote on last edited by
                        #11

                        I will check. In my code i use it to make another function call in class B and all is well. However, the call in questiuon here is actually used within an "if" block. Scope should be ok though. I will check on it.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups