| | |
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:
Solved Threads: 0
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
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
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.
•
•
Join Date: Feb 2008
Posts: 6
Reputation:
Solved Threads: 0
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!
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.
•
•
Join Date: Feb 2008
Posts: 6
Reputation:
Solved Threads: 0
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 -------".
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 -------".
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.
•
•
Join Date: Feb 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
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
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.
•
•
Join Date: Feb 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: Simple Integer Calulator
- Next Thread: Need help with matrix class (adding, printout,...etc)
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






