ACE: Sharing Dynamically Allocated Memory Across Threads

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 6
Reputation: Greg123 is an unknown quantity at this point 
Solved Threads: 0
Greg123 Greg123 is offline Offline
Newbie Poster

ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #1
Feb 26th, 2008
Hello,

I have dynamically allocated memory that I would like to share across threads. More specifically, I have an ACE thread pool, which I use to process requests on dynamically allocated ACE_Method_Request objects. These objects are created once at startup, and then processed throughout the system's running state (i.e. the same object is dispatched to different threads on a regular basis during system execution).

I am getting the following error:

Unhandled exception at 0xff5d7230 in Test.exe: 0xC0000005: Access violation reading location 0xff5d7230.

Btw, the dispatched objects form a hierarchy of classes. The crash seems not to occur when the code that processes the thread's request is in the parent abstract class and does not attempt to access any members from the corresponding child class. Virtual functions do not work, and I tried to use functors/function pointers to static functions defined in base classes and that does not work either.

Any thoughts?

Thanks!
Greg
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: Greg123 is an unknown quantity at this point 
Solved Threads: 0
Greg123 Greg123 is offline Offline
Newbie Poster

Re: ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #2
Feb 26th, 2008
Actually, to clarify, the crash seems to occur when an attempt to access the object's pointer (not its member data) occurs. Of course, accessing the object's member data can only occur after we access the object's pointer.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #3
Feb 26th, 2008
when sharing memory among threads you must synchronize access so that only one thread at a time has access to the object(s). One way to do that is to create a semiphore. How to do that depends on the operating system you are using, and possibly the compiler. Do a google search for semaphore and you will probably find what you need for your os/compiler.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: Greg123 is an unknown quantity at this point 
Solved Threads: 0
Greg123 Greg123 is offline Offline
Newbie Poster

Re: ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #4
Feb 26th, 2008
Hey!

Thanks for the reply. Although I agree with you that semaphores are needed, this does not seem to be the current issue.

In Debug mode, the "Access Violation" message always lists the address of this object + 4.

The following Wikipedia article indicates that it is the address of the first attribute in the object: http://en.wikipedia.org/wiki/Virtual_table

Thanks!
Last edited by Greg123; Feb 26th, 2008 at 7:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: Greg123 is an unknown quantity at this point 
Solved Threads: 0
Greg123 Greg123 is offline Offline
Newbie Poster

Re: ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #5
Feb 27th, 2008
To provide further information, the assembly code looks as follows:

printf ("DEBUG: mRegisteredComputeFunction = %p\n", mRegisteredComputeFunction);
00417E73 mov esi,esp
00417E75 mov eax,dword ptr [this]
00417E78 mov ecx,dword ptr [eax+4] ------- CRASH -------
00417E7B push ecx
00417E7C push offset string "DEBUG: mRegisteredComputeFunctio"... (420F20h)
00417E81 call dword ptr [__imp__printf (4255DCh)]
00417E87 add esp,8
00417E8A cmp esi,esp
00417E8C call @ILT+1250(__RTC_CheckEsp) (4114E7h)

mRegisteredComputeFunction (this);
00417E91 mov esi,esp
00417E93 mov eax,dword ptr [this]
00417E96 push eax
00417E97 mov ecx,dword ptr [this]
00417E9A mov edx,dword ptr [ecx+4]
00417E9D call edx
00417E9F add esp,4
00417EA2 cmp esi,esp
00417EA4 call @ILT+1250(__RTC_CheckEsp) (4114E7h)

The crash occurs at the third line marked with "------- CRASH -------".
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #6
Feb 27th, 2008
Can you duplicate the problem with a simple test program? According to the assembly code you posted it looks as if you pass the 'this' pointer to a non-class function or to a static function of a class.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: Greg123 is an unknown quantity at this point 
Solved Threads: 0
Greg123 Greg123 is offline Offline
Newbie Poster

Re: ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #7
Feb 27th, 2008
Originally Posted by Ancient Dragon View Post
Can you duplicate the problem with a simple test program? According to the assembly code you posted it looks as if you pass the 'this' pointer to a non-class function or to a static function of a class.
Hi,

Thanks for your reply.

Yes, I am passing it to a static function that is implemented in a child class, which "this" pointer should be an instance of. This was an attempt for a workaround so that I do not call virtual functions because I had suspected that they might have caused this issue. Unfortunately, the issue persists in both ways.

But the thing is it even crashes before I call this static function. Interestingly enough, however, it does not crash if I simply remove the function call - although it might crash if I run the program long "enough"!

Cheers.
Last edited by Greg123; Feb 27th, 2008 at 2:04 pm. Reason: correction
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #8
Feb 27th, 2008
My guess is that you are trashing memory some place -- could be buffer overruns, out-of-bounds problem or any number of other things. One way I've used to narrow down the problem is to comment out large blocks of code until I eventually find where the program breaks. Of course the first resort is to use my compiler's debugger to inspect variable values.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: Greg123 is an unknown quantity at this point 
Solved Threads: 0
Greg123 Greg123 is offline Offline
Newbie Poster

Re: ACE: Sharing Dynamically Allocated Memory Across Threads

 
0
  #9
Feb 27th, 2008
Originally Posted by Ancient Dragon View Post
My guess is that you are trashing memory some place -- could be buffer overruns, out-of-bounds problem or any number of other things. One way I've used to narrow down the problem is to comment out large blocks of code until I eventually find where the program breaks. Of course the first resort is to use my compiler's debugger to inspect variable values.
Hey dude!

Thanks so much. It does seem like a buffer overrun. I temporarily increased the size of arrays allocated using new() in which data is calculated and stored, and the issue does not occur anymore!

Greg
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC