I can't a class members in working thread

V

Vajkay.Rene

Hi

Last week I got faced to a curious problem when try to access the
member pointers of my class in the worker thread. In main thread
everything is ok, but in worker thread when I try to access member
pointers I got access violation failure. The curious part is that the
pointers addresses are ok, but the place where their points seem to be
empty in the worker thread but not in main thread.

Code:
class Sound
{
// Public Member Functions

protected:
static DWORD WINAPI ThreadProc(LPVOID lpParameter) { // Helper
function
Sound* info = (Sound*)&lpParameter;
return info->ThreadProc();
};
DWORD WINAPI ThreadProc();

LPDIRECTSOUNDBUFFER8 m_lpdsb8; // OK in main thread, fails in
worker
DSBUFFERDESC m_dsbdesc; // OK in both threads
LPDIRECTSOUND8 m_lpds; // I don't use it in worker thread
HANDLE m_hEvent[2]; // OK in main thread, fails in worker
SndFile* m_Wave; // OK in main thread, fails in worker
};

DWORD WINAPI Sound::ThreadProc()
{
DWORD dwBytes1, dwBytes2;
LPVOID lpvPtr1, lpvPtr2;
DWORD dwRetIndex = 0;
HRESULT hr = DS_OK;
DWORD dwOffset;

// m_hEvent address is ok but data seems to be empty
while ((dwRetIndex = WaitForMultipleObjects( 2, m_hEvent, FALSE,
INFINITE )) != WAIT_FAILED)
{
// ACCESS VIOLATION: m_lpdsb8
hr = m_lpdsb8->Lock( dwOffset, m_dsbdesc.dwSize / 2, &lpvPtr1,
&dwBytes1, &lpvPtr2, &dwBytes2, 0 );

// And so on
}

return 0;
}

The data of pointers should be correct because worker thread
terminates almost immediately(because the WaitForMultipleObjects
returns WAIT_FAILED for m_hEvent) and later when the main thread
access them everything goes fine.

Any idea?
 
J

Joe Greer

Code:
class Sound
{
// Public Member Functions

protected:
static DWORD WINAPI ThreadProc(LPVOID lpParameter) { // Helper
function
Sound* info = (Sound*)&lpParameter;

This doesn't look right to me. Shouldn't it be:
Sound* info = (Sound*)lpParameter;

???? You should already be passing a pointer to ThreadProc() taking the address of that pointer
is going to give you odd results.

Otherwise, I don't think there is quite enough info.

joe
 
A

Andre Kostur

(e-mail address removed) wrote in @q3g2000prf.googlegroups.com:
Hi

Last week I got faced to a curious problem when try to access the
member pointers of my class in the worker thread. In main thread
everything is ok, but in worker thread when I try to access member
pointers I got access violation failure. The curious part is that the
pointers addresses are ok, but the place where their points seem to be
empty in the worker thread but not in main thread.

Code:
class Sound
{
// Public Member Functions

protected:
static DWORD WINAPI ThreadProc(LPVOID lpParameter) { // Helper
function
Sound* info = (Sound*)&lpParameter;
return info->ThreadProc();
};
DWORD WINAPI ThreadProc();

LPDIRECTSOUNDBUFFER8 m_lpdsb8; // OK in main thread, fails in
worker
DSBUFFERDESC m_dsbdesc; // OK in both threads
LPDIRECTSOUND8 m_lpds; // I don't use it in worker thread
HANDLE m_hEvent[2]; // OK in main thread, fails in worker
SndFile* m_Wave; // OK in main thread, fails in worker
};

DWORD WINAPI Sound::ThreadProc()
{
DWORD dwBytes1, dwBytes2;
LPVOID lpvPtr1, lpvPtr2;
DWORD dwRetIndex = 0;
HRESULT hr = DS_OK;
DWORD dwOffset;

// m_hEvent address is ok but data seems to be empty
while ((dwRetIndex = WaitForMultipleObjects( 2, m_hEvent, FALSE,
INFINITE )) != WAIT_FAILED)
{
// ACCESS VIOLATION: m_lpdsb8
hr = m_lpdsb8->Lock( dwOffset, m_dsbdesc.dwSize / 2, &lpvPtr1,
&dwBytes1, &lpvPtr2, &dwBytes2, 0 );

// And so on
}

return 0;
}

The data of pointers should be correct because worker thread
terminates almost immediately(because the WaitForMultipleObjects
returns WAIT_FAILED for m_hEvent) and later when the main thread
access them everything goes fine.


See the C++ FAQ Lite: http://www.parashift.com/c++-faq-lite/pointers-to-
members.html

Section 33.2.
 
V

Vajkay.Rene

(e-mail address removed) wrote in




This doesn't look right to me. Shouldn't it be:
Sound* info = (Sound*)lpParameter;

???? You should already be passing a pointer to ThreadProc() taking the address of that pointer
is going to give you odd results.

Otherwise, I don't think there is quite enough info.

joe

Hello

Thank you for your correction, but both solutions are working on VC++
8.0 compiler.
Otherwise your is the right way.
 
V

Vajkay.Rene

(e-mail address removed) wrote in @q3g2000prf.googlegroups.com:




Last week I got faced to a curious problem when try to access the
member pointers of my class in the worker thread. In main thread
everything is ok, but in worker thread when I try to access member
pointers I got access violation failure. The curious part is that the
pointers addresses are ok, but the place where their points seem to be
empty in the worker thread but not in main thread.
Code:
class Sound
{
// Public Member Functions
protected:
static DWORD WINAPI ThreadProc(LPVOID lpParameter) { // Helper
function
Sound* info = (Sound*)&lpParameter;
return info->ThreadProc();
};
DWORD WINAPI ThreadProc();
LPDIRECTSOUNDBUFFER8 m_lpdsb8; // OK in main thread, fails in
worker
DSBUFFERDESC m_dsbdesc; // OK in both threads
LPDIRECTSOUND8 m_lpds; // I don't use it in worker thread
HANDLE m_hEvent[2]; // OK in main thread, fails in worker
SndFile* m_Wave; // OK in main thread, fails in worker
};
DWORD WINAPI Sound::ThreadProc()
{
DWORD dwBytes1, dwBytes2;
LPVOID lpvPtr1, lpvPtr2;
DWORD dwRetIndex = 0;
HRESULT hr = DS_OK;
DWORD dwOffset;
// m_hEvent address is ok but data seems to be empty
while ((dwRetIndex = WaitForMultipleObjects( 2, m_hEvent, FALSE,
INFINITE )) != WAIT_FAILED)
{
// ACCESS VIOLATION: m_lpdsb8
hr = m_lpdsb8->Lock( dwOffset, m_dsbdesc.dwSize / 2, &lpvPtr1,
&dwBytes1, &lpvPtr2, &dwBytes2, 0 );
// And so on
}
return 0;
}
The data of pointers should be correct because worker thread
terminates almost immediately(because the WaitForMultipleObjects
returns WAIT_FAILED for m_hEvent) and later when the main thread
access them everything goes fine.

See the C++ FAQ Lite:http://www.parashift.com/c++-faq-lite/pointers-to-
members.html

Section 33.2.- Hide quoted text -

- Show quoted text -

Thanks nice article, I read it. But it doesn't solve my problem. I
have problems with member-pointers-to-variables(to a event array and
to a directx object) that I'm unable to access in new thread.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top