Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++11 从boost::进程间共享内存访问boost::无序映射数据时出现Seg故障_C++11_Segmentation Fault_Containers_Unordered Map_Boost Interprocess - Fatal编程技术网

C++11 从boost::进程间共享内存访问boost::无序映射数据时出现Seg故障

C++11 从boost::进程间共享内存访问boost::无序映射数据时出现Seg故障,c++11,segmentation-fault,containers,unordered-map,boost-interprocess,C++11,Segmentation Fault,Containers,Unordered Map,Boost Interprocess,统计 有人能帮我弄清楚如何从boost共享内存中获取完整的地图内容吗 映射的键为int,值为struct。 结构包含容器(本例中为向量) 提前谢谢。 马扬克与您的问题无关,但C++中,对于结构或类,您不需要 TyPufF。结构和类名本身就是一个类型名。至于崩溃,你怎么知道它发生在你声称的地方?回溯跟踪不包含来自main函数的任何调试信息,它应该包含文件名和行号。在构建时,添加-g标志以便获得调试信息。我已经使用-g标志构建了,这是我得到的:(gdb)bt#0 0x00007ffff6d822c7

统计

有人能帮我弄清楚如何从boost共享内存中获取完整的地图内容吗

映射的键为int,值为struct。 结构包含容器(本例中为向量)

提前谢谢。
马扬克

与您的问题无关,但C++中,对于结构或类,您不需要<代码> TyPufF。结构和类名本身就是一个类型名。至于崩溃,你怎么知道它发生在你声称的地方?回溯跟踪不包含来自
main
函数的任何调试信息,它应该包含文件名和行号。在构建时,添加
-g
标志以便获得调试信息。我已经使用-g标志构建了,这是我得到的:
(gdb)bt#0 0x00007ffff6d822c7 in raise()from/usr/lib64/libc.so.6#1 0x00007ff6d839b8 in abort()from/usr/lib64/libc.so.6#2 0x00007ff76becf3 in"uu gnu cxx:"verbu在../../../../../../../../libstdc++-v3/libsupc++/vterminate.cc:95#3 0x00007ff76c4e66在../../../../libstdc++-v3/libcxsupc++/eh#terminate.cc:47#4 0x00007ff76c4ea1在../../../../../../libstdc++-v3/libsupc++/eh cxupc++/eh位于../../../.././libstdc++-v3/libsupc++/eh#throw.cc:133
\6 0x0000000000401dfc,位于ex#gate\u map\u client.cpp:35(gdb)
#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;

#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;
}

#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;
}

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)