static_cast and reinterpret_cast
-
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?
-
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?
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:
-
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?
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)
-
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?
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[^]
-
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[^]
-
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?
-
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?
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]; }
-
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]; }
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.
-
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.
-
are you sure arrPtr[i] contains valid object? debug it or show the code that fills the array.
-
are you sure arrPtr[i] contains valid object? debug it or show the code that fills the array.