0

Stat.h

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

typedef struct
{
unsigned int  gateType;
unsigned int  inPins;
unsigned int  outPins;
std::vector<int>  inWires;
std::vector<int>  outWires;
} gate;

gate gatedata;

    using namespace boost::interprocess;

    //create allocator
    typedef std::pair<const int, gate> gate_pair;
    typedef allocator<gate_pair, managed_shared_memory::segment_manager>gate_alloc;

   //create map data
   typedef boost::unordered_map<const int, gate, boost::hash<int > ,std::equal_to<int >, gate_alloc> gate_map;

******Server side**:**** server.cpp

#include "stat.h"

int main()
{
try {
    shared_memory_object::remove("MySharedMemory");
   //create the shared memory 
    managed_shared_memory segment(create_only, "MySharedMemory", 65536); 


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

    //construct the segment for pushing the data into it
    gate_map *gate_data = segment.construct<gate_map>("gatedata")(3, boost::hash<int>(), std::equal_to<int>(), alloc_inst);

    int i=0;
    gatedata.gateType = 1;
    gatedata.inPins = 2;
    gatedata.outPins = 3;

    for(int i=0;i<10;i++)
    {
        gatedata.inWires.push_back(i);
    gatedata.outWires.push_back(i);
    }

    //Now data is ready to be pushed inside map
    gate_pair gp(1, gatedata);
    gate_pair gp1(2, gatedata);
    gate_pair gp2(3, gatedata);
    gate_data->insert(gp);
    gate_data->insert(gp1);
    gate_data->insert(gp2);

    sleep(3);

}

catch (...) {
shared_memory_object::remove("MySharedMemory");
throw;
}
shared_memory_object::remove("MySharedMemory");
return 0;
}

Client side: client.cpp

#include "stat.h"

int main()
{
try {
   //create the shared memory 
    managed_shared_memory segment(open_only, "MySharedMemory"); 


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

    //construct the segment for pushing the data into it
    gate_map *gate_data = segment.find<gate_map>("gatedata").first;

    for (gate_map::iterator itr = gate_data->begin() ; itr != gate_data->end() ; ++itr)
    {
    std::cout << "key is: " << itr->first;
    std::cout << "Value is: " << itr->second.gateType << ", " << itr->second.inPins << "," << itr->second.outPins << " , ";

    std::cout << "inWires vector size: " << itr->second.inWires.size() << "\t";
    std::cout << "outWires vector size: " << itr->second.outWires.size() << "\t";

    for(int j=0 ; j < itr->second.outWires.size(); ++j){
                std::cout << itr->second.outWires.at(j); //Seg fault here!!!
        }
    std::cout << "\n";
    }

    segment.destroy<gate_map>("gate_map");
}

catch (...) {
shared_memory_object::remove("MySharedMemory");
throw;
}
shared_memory_object::remove("MySharedMemory");
return 0;
}

Problem: I am getting core dump in client side when i try to access the vector at this line:

std::cout << itr->second.outWires.at(j);

Below is the core dump:

No symbol "gate_data" in current context.
(gdb) bt
#0  0x00007ffff6d822c7 in raise () from /usr/lib64/libc.so.6
#1  0x00007ffff6d839b8 in abort () from /usr/lib64/libc.so.6
#2  0x00007ffff76becf3 in __gnu_cxx::__verbose_terminate_handler () at ../../.././libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff76c4e66 in __cxxabiv1::__terminate(void (*)()) () at ../../.././libstdc++-v3/libsupc++/eh_terminate.cc:47
#4  0x00007ffff76c4ea1 in std::terminate () at ../../.././libstdc++-v3/libsupc++/eh_terminate.cc:57
#5  0x00007ffff76c5129 in __cxxabiv1::__cxa_rethrow () at ../../.././libstdc++-v3/libsupc++/eh_throw.cc:133
#6  0x0000000000401dfc in main ()
(gdb) 

Can someone please help me to figure out how exactly to fetch the full map content from boost shared memory?

Map has key as int and value as struct. Struct contain containers(vectors in this case).

Thanks in advance. Mayank

  • Unrelated to your problem, but in C++ you don't need `typedef` for structures or classes. The structure and class name is itself a type-name. – Some programmer dude Dec 13 '19 at 11:53
  • As for the crash, how do you know that it happens where you claim? The back-trace doesn't contain any debug information from the `main` function, it should contain file name and line number. When building, add the `-g` flag so you get debug information. – Some programmer dude Dec 13 '19 at 11:55
  • I have built with -g flag and this is what i got: – Mayank Jain Dec 16 '19 at 06:00
  • `(gdb) bt #0 0x00007ffff6d822c7 in raise () from /usr/lib64/libc.so.6 #1 0x00007ffff6d839b8 in abort () from /usr/lib64/libc.so.6 #2 0x00007ffff76becf3 in __gnu_cxx::__verbose_terminate_handler () at ../../.././libstdc++-v3/libsupc++/vterminate.cc:95 #3 0x00007ffff76c4e66 in __cxxabiv1::__terminate(void (*)()) () at ../../.././libstdc++-v3/libsupc++/eh_terminate.cc:47 #4 0x00007ffff76c4ea1 in std::terminate () at ../../.././libstdc++-v3/libsupc++/eh_terminate.cc:57 #5 0x00007ffff76c5129 in __cxxabiv1::__cxa_rethrow () at ../../.././libstdc++-v3/libsupc++/eh_throw.cc:133` – Mayank Jain Dec 16 '19 at 06:05
  • `#6 0x0000000000401dfc in main () at ex_gate_map_client.cpp:35 (gdb) ` – Mayank Jain Dec 16 '19 at 06:05
  • Once i remove the 'std::cout << itr->second.outWires.at(j); ' Seg fault is not coming. – Mayank Jain Dec 16 '19 at 06:44
  • Did you manage to solve the problem? – aprospero Nov 26 '20 at 12:48
  • @aprospero It's still a problem. No solution yet. – Mayank Jain Dec 11 '20 at 11:25

0 Answers0