473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

I can't a class members in working thread

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?

Aug 30 '07 #1
4 1708
Va*********@gmail.com wrote in news:11**********************@q3g2000prf.googlegro ups.com:
>
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
Aug 30 '07 #2
Va*********@gmail.com wrote in news:1188502254.735748.218440
@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.

Aug 30 '07 #3
On Aug 30, 10:12 pm, Joe Greer <jgr...@doubletake.comwrote:
Vajkay.R...@gmail.com wrote innews:11**********************@q3g2000prf.googleg roups.com:
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
Hello

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

Aug 31 '07 #4
On Aug 30, 10:31 pm, Andre Kostur <nntps...@kostur.netwrote:
Vajkay.R...@gmail.com wrote in news:1188502254.735748.218440
@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.- 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.

Aug 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Niyazi | last post by:
Hi, I created application that I get information from AS400 for reporting. In main.exe has only 1 frm which calls (as a class library) CLS_MAIN.dll. The CLS_MAIN.dll get the tables from AS400...
3
by: Gianni Mariani | last post by:
I was a little surprised by this: It seems like the code below should not compile but the Comeau 4.3.3 compiler accepts it and the gcc 3.4(prerel) compiler rejects it and MSVC++7.1 ICE's. ...
44
by: Frank Rizzo | last post by:
Any ideas?
6
by: Plamen Doykov | last post by:
Hi all I have converted a simple project from ASP.NET 1 to 2.0 with the latest prerelease of Visual Studio 2005. The problem is I can't access internal members from the code behind. It gives:...
17
by: rory | last post by:
Hi everyone. I'm trying to declare a function as a friend in one of my classes. Correct me if I'm wrong but if I declare a friendly function that means that I can access member of my class in that...
6
by: joosteto | last post by:
Subject: pointer to any member function of any class. The "C++ FAQ Lite" explains how to hand a pointer to a member function to a signal handler etc, but only to a static function (33.2), to a...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
23
by: bc90021 | last post by:
Hi All, Thanks in advance for any and all help! I have this code: g = open(fileName, 'a') where fileName is defined before the line it's used in. It works fine when I use it outside a...
3
by: Michael Schöller | last post by:
Hello, First of all english is not my natural language so please fogive me some bad mistakes in gramatic and use of some vocables :). I have a great problem here. Well I will not use it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.