Segmentation fault when using std::string in stl::maps in shared memory
Hello All,
I have been trying boost library for sharing stl::maps using shared memory.
I am able to run the example code on sharing stl::maps successfully. It uses
int-float pair to store in map. I have to store std::string-int pair.
Writing into the map, works fine, yet reading the same from a different
process results in Segmentation fault.
Both the Process are as follows:
Process1.cpp
---------------------
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2007. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include
Hello All, After a few search I found out that we cannot use std::string. Instead we should try with interprocess::basic_string. Can anyone guide me through this. hamsinv wrote:
Hello All,
I have been trying boost library for sharing stl::maps using shared memory. I am able to run the example code on sharing stl::maps successfully. It uses int-float pair to store in map. I have to store std::string-int pair. Writing into the map, works fine, yet reading the same from a different process results in Segmentation fault.
Both the Process are as follows: Process1.cpp --------------------- ////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2006-2007. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/interprocess for documentation. // ////////////////////////////////////////////////////////////////////////////// #include
#include "boost/interprocess/detail/config_begin.hpp" #include "boost/interprocess/detail/workaround.hpp" //[doc_map #include "boost/interprocess/managed_shared_memory.hpp" #include "boost/interprocess/containers/map.hpp" #include "boost/interprocess/allocators/allocator.hpp" #include <functional> #include <utility> int main () { using namespace boost::interprocess; shared_memory_object::remove("MySharedMemory"); try{ managed_shared_memory segment (create_only ,"MySharedMemory" //segment name ,65536); //segment size in bytes typedef int KeyType; typedef std::string MappedType; typedef std::pair
ValueType; typedef allocator ShmemAllocator; typedef map MyMap; ShmemAllocator alloc_inst (segment.get_segment_manager()); MyMap *mymap = segment.construct<MyMap>("MyMap") //object name (std::less<int>() //first ctor parameter ,alloc_inst); //second ctor parameter //Insert data in the map for(int i = 0; i < 10; ++i){ cout<<"I value is: "<insert(std::pair
(i,"Hello")); } mymap = segment.find<MyMap>("MyMap").first; cout<<"Shared memory map created"<< mymap< begin();it != mymap->end();++it) { cout<<"key: "<<(*it).first<<" Value: "<<(*it).second< Process2.cpp -------------------- ////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2006-2007. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/interprocess for documentation. // ////////////////////////////////////////////////////////////////////////////// #include
#include "boost/interprocess/detail/config_begin.hpp" #include "boost/interprocess/detail/workaround.hpp" //[doc_map #include "boost/interprocess/managed_shared_memory.hpp" #include "boost/interprocess/containers/map.hpp" #include "boost/interprocess/allocators/allocator.hpp" #include <functional> #include <utility> int main () { using namespace boost::interprocess; try{ managed_shared_memory segment (open_only ,"MySharedMemory" //segment name ); typedef int KeyType; typedef std::string MappedType; typedef std::pair
ValueType; typedef allocator ShmemAllocator; typedef map MyMap; MyMap *mymap = segment.find<MyMap>("MyMap").first; //object name for( MyMap::iterator it = mymap->begin();it != mymap->end();++it) { cout<<"key: "<<(*it).first<<" Value: "<<(*it).second< Could anyone please tell me what's going wrong and how can i use std::string as key and int as value.
Thank you.
-- View this message in context: http://www.nabble.com/Segmentation-fault-when-using-std%3A%3Astring-in-stl%3... Sent from the Boost - Users mailing list archive at Nabble.com.
On Monday 20 October 2008 02:38:05 hamsinv wrote:
After a few search I found out that we cannot use std::string. Instead we should try with interprocess::basic_string. Can anyone guide me through this.
The fundamental issue is that objects which allocate memory on the heap cannot be used with shared memory. Consider what happens when you use a construct a string in shared memory. Upon construction which requires storage (such as assigning it an initial value), the string constructor allocates some memory on the heap (not in the shared memory segment), which is visible only in the constructing process. Using the memory in the other processes tries to access those memory locations which results in undefined behavior. In order to overcome this problem, you need to use a shared-memory-aware string, such as interprocess::basic_string. Please read the section named "Limitations When Constructing Objects In Mapped Regions" in the interprocess manual for more information. Regards, Ravi
Thank you for the pointer. Could you please explain how to use boost::interprocess::string. Regards Ravi-41 wrote:
On Monday 20 October 2008 02:38:05 hamsinv wrote:
After a few search I found out that we cannot use std::string. Instead we should try with interprocess::basic_string. Can anyone guide me through this.
The fundamental issue is that objects which allocate memory on the heap cannot be used with shared memory. Consider what happens when you use a construct a string in shared memory. Upon construction which requires storage (such as assigning it an initial value), the string constructor allocates some memory on the heap (not in the shared memory segment), which is visible only in the constructing process. Using the memory in the other processes tries to access those memory locations which results in undefined behavior. In order to overcome this problem, you need to use a shared-memory-aware string, such as interprocess::basic_string.
Please read the section named "Limitations When Constructing Objects In Mapped Regions" in the interprocess manual for more information.
Regards, Ravi
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/Segmentation-fault-when-using-std%3A%3Astring-in-stl%3... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (2)
-
hamsinv
-
Ravi