[Interprocess] Different outputs under bash and gdb
Dear all, I have a question about proper use of Boost.Interprocess for sharing custom C++ classes. I recently started using Boost.Interprocess and have been successful in sharing boost::array, std::vector<float>, std::vector< boost::array<float, 2> > etc using boost::interprocess::managed_shared_memory and boost::interprocess::allocator. Currently, I am trying to share a custom matrix class that is used to hold data in a tabular format. The Matrix class (attached header file Matrix.hpp) is a simple wrapper class that uses std::vector to store objects and provides functions to find items using linear and matrix indexing. I have modified a boost::interprocess example program to try out sharing the Matrix class (attached file test_matrix_shm.cpp). The parent process creates a managed memory segment along with a Matrix in the shared memory. The child process accesses the shared memory and successfully finds the Matrix object. However, when I run the program under bash, the output of the child process shows wrong data. However, if I run the same program under gdb, then every time the child process shows correct Matrix data as was set by the parent process (please find sample outputs below). I am really not sure what would cause this behavior. I would appreciate any input in this regard. * bash output * parent process Matrix size = 2 x 2 0 1 2 3 child process Matrix size = 2 x 2 -1.2522e-41 -1.25486e-41 -1.2571e-41 -1.25949e-41 child returning parent returning * gdb output * GNU gdb (GDB) SUSE (7.1-3.12) (gdb) run Starting program: test_matrix_shm parent process Matrix size = 2 x 2 0 1 2 3 Detaching after fork from child process 2548. child process Matrix size = 2 x 2 0 1 2 3 child returning parent returning Program exited normally. Thanks - Aditya
On 23/11/2010 22:23, Aditya Gadre wrote:
Dear all,
I have a question about proper use of Boost.Interprocess for sharing custom C++ classes. I recently started using Boost.Interprocess and have been successful in sharing boost::array, std::vector<float>, std::vector< boost::array<float, 2> > etc using boost::interprocess::managed_shared_memory and boost::interprocess::allocator.
You can't share std::vector, it contains raw pointers. You must use boost::interprocess:vector with boost::interprocess::allocator. Best, ion
Ion, Thank you for your reply. According to Boost.Interprocess documentation, you *can* share std::vector by using managed shared memory and Boost.Interprocess allocator (please see "Creating vectors in shared memory" in "Quick guide for the impatient" in Boost.Interprocess documentation). Following those guidelines and the example therein , I have successfully shared std::vector object using following - using boost::interprocess; typedef allocator<float, managed_shared_memory::segment_manager> ShmAllocator_float; typedef std::vector<float, ShmAllocator_float> MyVector; managed_shared_memory segment(create_only, "MySharedVector", 10000); const ShmAllocator_float alloc(segment.get_segment_manager()); MyVector* pvec = segment.construct<MyVector>("MyVector")(alloc); - Aditya On 11/24/2010 04:47 AM, Ion Gaztañaga wrote:
On 23/11/2010 22:23, Aditya Gadre wrote:
Dear all,
I have a question about proper use of Boost.Interprocess for sharing custom C++ classes. I recently started using Boost.Interprocess and have been successful in sharing boost::array, std::vector<float>, std::vector< boost::array<float, 2> > etc using boost::interprocess::managed_shared_memory and boost::interprocess::allocator.
You can't share std::vector, it contains raw pointers. You must use boost::interprocess:vector with boost::interprocess::allocator.
Best,
ion _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 24/11/2010 15:21, Aditya Gadre wrote:
Ion,
Thank you for your reply. According to Boost.Interprocess documentation, you *can* share std::vector by using managed shared memory and Boost.Interprocess allocator (please see "Creating vectors in shared memory" in "Quick guide for the impatient" in Boost.Interprocess documentation). Following those guidelines and the example therein , I have successfully shared std::vector object using following -
Believe me, you can't. The example uses boost::interprocess::vector (see using namespace boost::interprocess; and not using namespace std;) http://www.boost.org/doc/libs/1_45_0/doc/html/interprocess/quick_guide.html#... you can't share a std::vector because its internal pointers are normally raw pointers. If two processes map the segment in a different base address it will crash. If you are lucky and both processes map the segment in the same address, then it won't crash, but you have no guarantee for that. For more explanations see: http://www.boost.org/doc/libs/1_45_0/doc/html/interprocess/allocators_contai... Best, Ion
Ion, My appologies...I did not realize that typedef refers to boost::interprocess::vector instead of std::vector. So now I have this new question - what would one need to do in order to share his/her custom C++ classes. Thanks, - Aditya On 11/24/2010 04:47 AM, Ion Gaztañaga wrote:
On 23/11/2010 22:23, Aditya Gadre wrote:
Dear all,
I have a question about proper use of Boost.Interprocess for sharing custom C++ classes. I recently started using Boost.Interprocess and have been successful in sharing boost::array, std::vector<float>, std::vector< boost::array<float, 2> > etc using boost::interprocess::managed_shared_memory and boost::interprocess::allocator.
You can't share std::vector, it contains raw pointers. You must use boost::interprocess:vector with boost::interprocess::allocator.
Best,
ion _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 24/11/2010 15:41, Aditya Gadre wrote:
Ion,
My appologies...I did not realize that typedef refers to boost::interprocess::vector instead of std::vector. So now I have this new question - what would one need to do in order to share his/her custom C++ classes.
Replace raw pointers with offset_ptr's and avoid storing references and static members and avoid virtuals. See documentation ("Limitations When Constructing Objects In Mapped Regions") for that: http://www.boost.org/doc/libs/1_45_0/doc/html/interprocess/sharedmemorybetwe... Best, Ion
Replace raw pointers with offset_ptr's and avoid storing references and static members and avoid virtuals. See documentation ("Limitations When Constructing Objects In Mapped Regions") for that:
http://www.boost.org/doc/libs/1_45_0/doc/html/interprocess/sharedmemorybetwe...
Ion, The code I already have uses std::vector to store data and it may not be possible to replace it with interprocess::vector. Is there any other container that may be used that has the same interface as std::vector. In the worst case, I can write a very simple class to hold data. Thanks, - Aditya
On 24/11/2010 17:33, Aditya Gadre wrote:
The code I already have uses std::vector to store data and it may not be possible to replace it with interprocess::vector. Is there any other container that may be used that has the same interface as std::vector. In the worst case, I can write a very simple class to hold data.
No, sorry, you need to replace std::vector with an shared memory friendly class. boost::interprocess::vector is the only one I know. In the future (c++0x) std::vector will support boost::interprocess allocators but meanwhile, you'll need to change your code. Best, Ion
participants (2)
-
Aditya Gadre
-
Ion Gaztañaga