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<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<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.