Boost logo

Boost Users :

Subject: Re: [Boost-users] Segmentation fault when using std::string in stl::maps in shared memory
From: hamsinv (hamsinv_at_[hidden])
Date: 2008-10-20 02:38:05


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 <iostream.h>
> #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<const int, std::string> ValueType;
> typedef allocator<ValueType, managed_shared_memory::segment_manager>
> ShmemAllocator;
> typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator>
> 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: "<<i<<endl;
> mymap->insert(std::pair<const int,std::string>(i,"Hello"));
> }
> mymap = segment.find<MyMap>("MyMap").first;
> cout<<"Shared memory map created"<< mymap<<endl;
> for( MyMap::iterator it = mymap->begin();it != mymap->end();++it)
> {
> cout<<"key: "<<(*it).first<<" Value:
> "<<(*it).second<<endl;
> }
> }
> catch(...){
> shared_memory_object::remove("MySharedMemory");
> throw;
> }
> return 0;
> }
>
> 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 <iostream.h>
> #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<const int, std::string> ValueType;
> typedef allocator<ValueType, managed_shared_memory::segment_manager>
> ShmemAllocator;
> typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator>
> 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<<endl;
> }
> }
> catch(...){
> shared_memory_object::remove("MySharedMemory");
> throw;
> }
> return 0;
> }
>
> 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%3A%3Amaps-in-shared-memory-tp20063079p20063981.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net