mercredi 12 octobre 2022

Sharing std::string among the processes using Boost::Interprocess

I am following this Boost::Interprocess guide . I have structure which has std::string as member and I have created multimap of that structure with std::string as key values. I can access std::string from structure in producer code . But, when I try to access the same std::string member in consumer code then I am getting core dump.

Can some one help me. Thanks in advance. I am giving piece of code below :

Producer code :

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

struct temp
{
    int x;
    int y;
    std::string name;
};

    int main ()
{
    using namespace boost::interprocess;
   
    try
    {
        shared_memory_object::remove("MySharedMultimap");

        managed_shared_memory segment
         (create_only
         ,"MySharedMultimap" //segment name
         ,65536);          //segment size in bytes


        typedef std::string KeyType;
        typedef struct temp MappedType;
        typedef std::pair<const std::string, struct temp> ValueType;

        typedef allocator< ValueType, managed_shared_memory::segment_manager>
            ShmemAllocator;


        typedef multimap<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMultimap;

        //Initialize shared memory STL-compatible allocator
        const ShmemAllocator alloc_inst (segment.get_segment_manager());

          //Construct a shared memory
        MyMultimap *mymultimap =
             segment.construct<MyMultimap>("MyMultimap") //object name
                                        (alloc_inst);//first ctor parameter

        std::string s = "key1";
        mymultimap->insert(std::pair<KeyType, MappedType>(s, t1));
    }
    catch(...)
    {
          shared_memory_object::remove("MySharedMultimap");
          throw;
    }
}

Consumer code :

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>



struct temp
{
        int x;
        int y;
        std::string name;
};

    int main ()
   {
       using namespace boost::interprocess;
   
   try
   {
      managed_shared_memory segment
         (open_only
         ,"MySharedMultimap");  //segment name


      typedef std::string KeyType;
      typedef struct temp MappedType;
      typedef std::pair<const std::string, struct temp> ValueType;

      //Alias an STL compatible allocator of ints that allocates ints from the managed
      //shared memory segment.  This allocator will allow to place containers
      //in managed shared memory segments
      typedef allocator<ValueType , managed_shared_memory::segment_manager>
         ShmemAllocator;

      //Alias a vector that uses the previous STL-like allocator
      typedef multimap< KeyType, MappedType, std::less<KeyType>,  ShmemAllocator> MyMultimap;

      //Find the vector using the c-string name
      MyMultimap *mymultimap = segment.find<MyMultimap>("MyMultimap").first;

      std:: cout << " multimap size size : " << mymultimap->size() << std::endl;


      for(auto i = mymultimap->begin(); i != mymultimap->end(); ++i)
      {
        std::cout << i->first << std::endl;      // core dump ( as we are accessing std::string )

        std::cout << i->second.x << std::endl;  // works fine

        std::cout << i->second.name << std::endl;   // core dump ( as we are accessing std::string )
      }
    }
    catch(...){
      shared_memory_object::remove("MySharedMultimap");
      throw;
    }
}

Aucun commentaire:

Enregistrer un commentaire