C++ 程序在试图访问内部映射时崩溃

C++ 程序在试图访问内部映射时崩溃,c++,boost,stl,C++,Boost,Stl,下面是一个小的读写程序。写入程序将值写入映射的映射,写入成功。 还有另一个进程称为reader,它读取SHM的内容,读取器读取外部映射的键没有问题,但是如果我们试图访问内部映射的内容,它会崩溃。 我已经检查了容器大小,它为case1打印了2,这很好,因为内部映射包含键“case1”的2个元素 然而,当我试图访问内部映射时,程序崩溃,我访问内部映射的方式是否不正确。下面是我正在使用的代码片段 ShmTypedefs.h 核心输出: (gdb) bt #0 get_right () at /hom

下面是一个小的读写程序。写入程序将值写入映射的映射,写入成功。 还有另一个进程称为reader,它读取SHM的内容,读取器读取外部映射的键没有问题,但是如果我们试图访问内部映射的内容,它会崩溃。 我已经检查了容器大小,它为case1打印了2,这很好,因为内部映射包含键“case1”的2个元素

然而,当我试图访问内部映射时,程序崩溃,我访问内部映射的方式是否不正确。下面是我正在使用的代码片段

ShmTypedefs.h 核心输出:

(gdb) bt
#0  get_right () at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/rbtree_node.hpp:130
#1  next_node () at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/tree_algorithms.hpp:441
#2  next_node () at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/rbtree_algorithms.hpp:353
#3  operator++ () at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/tree_node.hpp:119
#4  prot_incr ()
    at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/detail/tree.hpp:326
#5  operator++ ()
    at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/detail/tree.hpp:395
#6  __distance<boost::container::containers_detail::rbtree<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int>, boost::container::containers_detail::select1st<std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int> >, std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >, std::allocator<std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int> > >::iterator> ()
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_iterator_base_funcs.h:79
#7  distance<boost::container::containers_detail::rbtree<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interproce---Type <return> to continue, or q <return> to quit---
ss::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int>, boost::container::containers_detail::select1st<std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int> >, std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >, std::allocator<std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int> > >::iterator> ()
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_iterator_base_funcs.h:114
#8  main () at /home/user/droy/src/quotes/debshmutils/shmmapofmap/src/reader.cxx:33
(gdb)bt
#0在/home/dev/build/third\u party/64-rhel5/boost\u 1\u 47\u 0/include/boost/intrusive/detail/rbtree\u node上获得正确的结果()。hpp:130
#下一个节点()位于/home/dev/build/third\u party/64-rhel5/boost\u 1\u 47\u 0/include/boost/intrusive/detail/tree\u algorithms.hpp:441
#下一个节点()位于/home/dev/build/third\u party/64-rhel5/boost\u 1\u 47\u 0/include/boost/intrusive/rbtree\u algorithms.hpp:353
#3 operator++()位于/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/tree_node.hpp:119
#4保护增量()
位于/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/detail/tree.hpp:326
#5运算符++()
在/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/detail/tree.hpp:395
#6距离()
at/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../../../../../include/c++/4.4.7/bits/stl_迭代器\u base\u funcs.h:79
#7距离()
在/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../../../../../include/c++/4.4.7/bits/stl_迭代器\u base\u funcs.h:114
#8 main()位于/home/user/droy/src/quotes/debshmutils/shmmapofmap/src/reader.cxx:33
您映射的类型:

typedef boost::interprocess::map<SharedString, int>  MappedType;
注:

  • 您在main中有一个冲突的MySHMMap定义
  • 这是非常笨拙的代码

    InnerMapAlloc inner_map_alloc_instance(segment.get_segment_manager());
    
    {
        MappedType& tmp = m_pmap->emplace(key, MappedType(inner_map_alloc_instance)).first->second;
        tmp.emplace(inner_map_key, 2030);
        tmp.emplace(inner_map_key2, 2034);
    }
    
    {
        MappedType& tmp = m_pmap->emplace(key, MappedType(inner_map_alloc_instance)).first->second;
        tmp.emplace(inner_map_key1, 2031);
    }
    
    for (auto const& outer : *m_pmap)
        for (auto const& inner : outer.second)
            std::cout << outer.first << ", " << inner.first << ", " << inner.second << "\n";
    
    我从没听说过这个。你能指出说明这一点的文件吗


您的代码太复杂,无法发现可能出错的地方,您能否简化为MWE?(请参见)看起来您想要读取的节点不包含exist@Maciej那么为什么内部映射的大小是2呢?示例很大,但我看到您保存了迭代器。您确定在执行距离函数时它们指向的元素仍然存在吗?代码行为MappedType::iterator iti=(iter->second).begin(),MappedType::iterator iti_end=(iter->second).end();我不认为迭代器失效的可能性,映射的映射在共享内存中,这可能是导致问题的原因吗?Hmmn。我应该有一个MappedType的分配器,就像SharedString一样。让我试试。@deb我已经(为了好玩和荣耀)为你修复了东西的作者端,请参阅更新。我认为你正淹没在共享分配的沼泽中。你能重新考虑一下设计吗?@deb和为了更有趣的事:。我认为这样更容易阅读和维护。
   droy@apdeva01 shmmapofmap> ./bin/reader
    size of map 1
    case1
    Matched case 1
    Size of the inner container is 2 units
    Segmentation fault (core dumped)
(gdb) bt
#0  get_right () at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/rbtree_node.hpp:130
#1  next_node () at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/tree_algorithms.hpp:441
#2  next_node () at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/rbtree_algorithms.hpp:353
#3  operator++ () at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/tree_node.hpp:119
#4  prot_incr ()
    at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/detail/tree.hpp:326
#5  operator++ ()
    at /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/detail/tree.hpp:395
#6  __distance<boost::container::containers_detail::rbtree<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int>, boost::container::containers_detail::select1st<std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int> >, std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >, std::allocator<std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int> > >::iterator> ()
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_iterator_base_funcs.h:79
#7  distance<boost::container::containers_detail::rbtree<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interproce---Type <return> to continue, or q <return> to quit---
ss::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int>, boost::container::containers_detail::select1st<std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int> >, std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >, std::allocator<std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > const, int> > >::iterator> ()
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_iterator_base_funcs.h:114
#8  main () at /home/user/droy/src/quotes/debshmutils/shmmapofmap/src/reader.cxx:33
typedef boost::interprocess::map<SharedString, int>  MappedType;
typedef boost::interprocess::allocator<std::pair<const SharedString, int>, boost::interprocess::managed_shared_memory::segment_manager > InnerMapAlloc;
typedef boost::interprocess::map<SharedString, int, std::less<SharedString>, InnerMapAlloc>  MappedType;
case1, inner_case1, 2030
case1, inner_case2, 2031
case1, inner_case3, 2034
InnerMapAlloc inner_map_alloc_instance(segment.get_segment_manager());

{
    MappedType& tmp = m_pmap->emplace(key, MappedType(inner_map_alloc_instance)).first->second;
    tmp.emplace(inner_map_key, 2030);
    tmp.emplace(inner_map_key2, 2034);
}

{
    MappedType& tmp = m_pmap->emplace(key, MappedType(inner_map_alloc_instance)).first->second;
    tmp.emplace(inner_map_key1, 2031);
}

for (auto const& outer : *m_pmap)
    for (auto const& inner : outer.second)
        std::cout << outer.first << ", " << inner.first << ", " << inner.second << "\n";
// After construction m_pmap will contain the only object that has been constructed in pmap[0]
// If we construct another object it will goto pmap[1]