试图访问内部地图时程序崩溃 [英] Program crashes when trying to access the inner map

查看:271
本文介绍了试图访问内部地图时程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个小的读写器程序。作家写值地图的地图,写成功。
 还有另一种称为读取器其中读取SHM的内容,读者在阅读外映射的键没有问题的过程,但是,如果我们试图访问它崩溃的内映射的内容。
  我检查了容器的大小和它案例1这是细如内部地图包含了关键的案例1。

2的元素打印2

然而,当我尝试访问到内部的地图程序崩溃,是我访问内部地图不正确的方式。以下是我使用的code片段。

ShmTypedefs.h

 的#ifndef __SHM_TYPE_DEFS__
    #定义__SHM_TYPE_DEFS__
    #包括LT&;升压/间/分配器/ allocator.hpp>
    #包括LT&;升压/间/ managed_shared_memory.hpp>
    #包括LT&;升压/间/集装箱/ string.hpp>
    #包括LT&;升压/间/集装箱/ map.hpp>    //串也需要被从共享存储器分配
    TYPEDEF的boost ::进程间::分配器<无效,升压::进程间:: managed_shared_memory :: segment_manager> VoidAllocator;
    TYPEDEF的boost ::进程间::分配器<焦炭,提高::进程间:: managed_shared_memory :: segment_manager> CharAllocator;
    TYPEDEF的boost ::进程间:: basic_string的<焦炭,的std :: char_traits<焦炭>中CharAllocator> SharedString;    //注意地图<关键,MappedType>的value_type为的std ::对< const的重点,MappedType>中
    //所以分配器必须分配的对。
    的typedef SharedString关键字类型;
    TYPEDEF的boost ::进程间::地图< SharedString,INT> MappedType;
    的typedef的std ::对<常量关键字类型,MappedType>值类型;    //分配字符串
    TYPEDEF的boost ::进程间::分配器< SharedString,提高::进程间:: managed_shared_memory :: segment_manager> StringAllocator;
    //为地图分配器。
    TYPEDEF的boost ::进程间::分配器<值类型,提高::进程间:: managed_shared_memory :: segment_manager> ShmemAllocator;
    TYPEDEF的boost ::进程间::分配器<值类型,提高::进程间:: managed_shared_memory :: segment_manager> ShmemAllocator;
    //第三个参数参数是排序功能用来比较的键。
    TYPEDEF的boost ::进程间::地图<关键字类型,MappedType,性病::少<关键字类型>中ShmemAllocator> MySHMMap;
#万一

writer.cxx

 的#include<升压/间/集装箱/ map.hpp>
    的#include<功能>
    #包括LT&;实用>
    #包括LT&;&ShmTypedefs.h GT;
    #包括LT&;&iostream的GT;
    使用空间boost ::进程间;    诠释的main()
    {
      //删除较早现有SHM
      shared_memory_object ::删除(SharedMemoryName);      // 创建新的
      managed_shared_memory段(create_only,SharedMemoryName,65536);      //初始化共享内存STL兼容分配器
      ShmemAllocator alloc_inst(segment.get_segment_manager());      //第三个参数参数是排序功能用来比较的键。
      typedef的地图和LT;关键字类型,MappedType,性病::少<关键字类型>中ShmemAllocator> MySHMMap;      // PTR偏移内SHM在地图
      //建设m_pmap后,将包含已被PMAP consturcted的唯一对象[0]
      //如果我们构造另一个对象,它会转到PMAP [1]      offset_ptr< MySHMMap> m_pmap = segment.construct< MySHMMap>(MySHMMapName)(的std ::以下< SharedString>(),alloc_inst);      在地图//插入数据
      StringAllocator string_alloc_instance(segment.get_segment_manager());      SharedString键(情形1,string_alloc_instance);
      SharedString inner_map_key(inner_case1,string_alloc_instance);      SharedString KEY1(情形2,string_alloc_instance);
      SharedString inner_map_key1(inner_case2,string_alloc_instance);      SharedString inner_map_key2(inner_case3,string_alloc_instance);      m_pmap [0] [重点] [inner_map_key] = 2030
      m_pmap [0] [密钥1] [inner_map_key1] = 2031;
      m_pmap [0] [重点] [inner_map_key2] = 2034;
      性病::法院LT&;<\\ n<< m_pmap [0] [重点] [inner_map_key<<的std :: ENDL;
      性病::法院LT&;<\\ n<< m_pmap [0] [键1] [inner_map_key1<<的std :: ENDL;
      返回0;
    }

reader.cxx

 的#include<升压/间/集装箱/ map.hpp>
的#include<功能>
#包括LT&;实用>
#包括LT&;&iostream的GT;
#包括LT&;&ShmTypedefs.h GT;使用空间boost ::进程间;诠释的main()
{  尝试
  {    managed_shared_memory段(open_or_create,SharedMemoryName,65536);    INT大小= segment.find< MySHMMap方式>(MySHMMapName)第二;
    性病::法院LT&;< &LT地图的大小;<大小和LT;<的std :: ENDL;
    offset_ptr< MySHMMap> m_pmap = segment.find< MySHMMap方式>(MySHMMapName)第一;
    MySHMMap:迭代ITER = m_pmap [0] .begin();
    MySHMMap:迭代iter_end = m_pmap [0] .END();
    对于(; ITER = iter_end;!++ ITER){
      性病::法院LT&;<\\ n<< iter->首先<<的std :: ENDL;
      如果(iter->首先==情形1){
         MappedType:迭代ITI =(iter->第二个).begin();         MappedType:迭代iti_end =(iter->第二个).END();
         对于(,ITI = iti_end;!++ ITI){
         性病::法院LT&;< 符号<< iti->首先<< 时间<< iti->第二个<<的std :: ENDL;
          }
      }
    }
  }
  赶上(性病::例外急症){
    性病::法院LT&;<错误<< e.what()<<的std :: ENDL;
    shared_memory_object ::删除(SharedMemoryName);
  }
  返回0;
}

堆栈跟踪:

输出当我运行该程序=>

  droy @ apdeva01 shmmapofmap> ./bin/reader
    图1的大小
    情况1
    匹配的情况下,1
    内部容器的大小是2个单位
    分段错误(核心转储)

核心输出:

 (GDB)BT
#0 get_right()在/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/rbtree_node.hpp:130
#1 next_node()在/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/tree_algorithms.hpp:441
#2 next_node()在/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/rbtree_algorithms.hpp:353
#3 ++运算符()在/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/intrusive/detail/tree_node.hpp:119
#4 prot_incr()
    在/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 __distance&LT;提高::容器:: containers_detail :: rbtree&LT;提高::容器:: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,提高::进程间:: segment_manager&LT ;焦炭,升压::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &gt;中的std ::对&LT;提高::容器:: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,提高::进程间:: segment_manager&LT;焦炭,提高::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &GT;常量,INT&gt;中boost::container::containers_detail::select1st<std::pair<boost::container::basic_string<char,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,升压::进程间:: segment_manager&LT;焦炭,提高::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &GT;常量,INT&GT; &gt;中的std ::以下&LT;提高::容器:: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,升压::进程间:: segment_manager&LT;焦炭,提高::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &GT; &gt;中的std ::分配器&LT;的std ::对&LT;提高::容器:: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,提高::进程间:: segment_manager&LT;焦炭,提高::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &GT;常量,INT&GT; &GT; &GT; ::迭代器&GT; ()
    在/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_iterator_base_funcs.h:79
#7的距离&LT;提高::容器:: containers_detail :: rbtree&LT;提高::容器:: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,提高::进程间:: segment_manager&LT ;焦炭,升压::进程间:: rbtree_best_fit&LT;提高:: interproce ---类型&lt;&回报GT;继续,或Q&LT;返回&GT;退出 - -
SS :: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &gt;中的std ::对&LT;提高::容器:: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,提高::进程间:: segment_manager&LT;焦炭,提高::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &GT;常量,INT&gt;中boost::container::containers_detail::select1st<std::pair<boost::container::basic_string<char,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,升压::进程间:: segment_manager&LT;焦炭,提高::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &GT;常量,INT&GT; &gt;中的std ::以下&LT;提高::容器:: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,升压::进程间:: segment_manager&LT;焦炭,提高::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &GT; &gt;中的std ::分配器&LT;的std ::对&LT;提高::容器:: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的boost ::进程间::分配器&LT;焦炭,提高::进程间:: segment_manager&LT;焦炭,提高::进程间:: rbtree_best_fit&LT;的boost ::进程间:: mutex_family,提高::进程间:: offset_ptr&LT;无效&gt;中0ul&gt;中的boost ::进程间:: iset_index&GT; &GT; &GT;常量,INT&GT; &GT; &GT; ::迭代器&GT; ()
    在/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_iterator_base_funcs.h:114
#8的main()在/home/user/droy/src/quotes/debshmutils/shmmapofmap/src/reader.cxx:33


解决方案

您映射类型:

 的typedef的boost ::进程间::地图&LT; SharedString,INT&GT; MappedType;

不会在共享存储器分配它的节点。因此,在外部地图的指针指向的地址上的本地堆,因此不存在(或未定义)上的读出侧。

下面是固定的作家 住在Coliru 。相关类型定义:

 的typedef的boost ::进程间::分配器&LT;的std ::对&LT;常量SharedString,INT&gt;中的boost ::进程间:: managed_shared_memory :: segment_manager&GT; InnerMapAlloc;
TYPEDEF的boost ::进程间::地图&LT; SharedString,INT的std ::以下&LT; SharedString&gt;中InnerMapAlloc&GT; MappedType;

输出:

 情形1,inner_case1年,2030年
情形1,inner_case2,2031
情形1,inner_case3,2034

请注意:


  • 您有主
  • 内部冲突的MySHMMap定义
  • 这是很笨拙code。

      InnerMapAlloc inner_map_alloc_instance(segment.get_segment_manager());{
        MappedType&安培; TMP = m_pmap-&GT;布设(键,MappedType(inner_map_alloc_instance))第一代方式&gt;第二位;
        tmp.emplace(inner_map_key,2030);
        tmp.emplace(inner_map_key2,2034);
    }{
        MappedType&安培; TMP = m_pmap-&GT;布设(键,MappedType(inner_map_alloc_instance))第一代方式&gt;第二位;
        tmp.emplace(inner_map_key1,2031);
    }为(自动常量和放大器;外:* m_pmap)
        为(自动常量和放大器;内部:outer.second)
            性病::法院LT&;&LT; outer.first&LT;&LT; ,&所述;&下; inner.first&LT;&LT; ,&所述;&下; inner.second&LT;&LT; \\ n;

    考虑的似地简化了数据结构和/或抽象它走在正确的类隐藏uglies

    更新发布基于助推的PoC多指标


  • 据我所知,

      //施工后m_pmap将包含已PMAP已建成的唯一对象[0]
    //如果我们构造另一个对象,它会转到PMAP [1]

    我从来没有听说过这一点。你能指出这何处规定的文件?


Following is a small reader writer program. The writer writes values to a map of maps, the write is successful. There is another process called reader which reads the contents of the SHM, the reader has no problem in reading the key of the outer map, however if we try to access the contents of the inner map it crashes. I have checked the container size and it prints 2 for case1 which is fine as the inner map contains 2 elements for the key "case1".

However, when I try to access to inner map the program crashes, is the way I am accessing the inner map not correct. Following are the code snippets that I am using.

ShmTypedefs.h

#ifndef __SHM_TYPE_DEFS__
    #define __SHM_TYPE_DEFS__
    #include <boost/interprocess/allocators/allocator.hpp>
    #include <boost/interprocess/managed_shared_memory.hpp>
    #include <boost/interprocess/containers/string.hpp>
    #include <boost/interprocess/containers/map.hpp>

    //the strings also need to be assigned from the shared memory
    typedef boost::interprocess::allocator<void, boost::interprocess::managed_shared_memory::segment_manager> VoidAllocator;
    typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
    typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> SharedString;

    //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
    //so the allocator must allocate that pair.
    typedef SharedString KeyType;
    typedef boost::interprocess::map<SharedString, int>  MappedType;
    typedef std::pair<const KeyType, MappedType> ValueType;

    //allocator for the string
    typedef boost::interprocess::allocator<SharedString, boost::interprocess::managed_shared_memory::segment_manager> StringAllocator;
    //allocator of for the map.
    typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
    typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
    //third parameter argument is the ordering function is used to compare the keys.
    typedef boost::interprocess::map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;
#endif

writer.cxx

    #include <boost/interprocess/containers/map.hpp>
    #include <functional>
    #include <utility>
    #include <ShmTypedefs.h>
    #include <iostream>
    using namespace boost::interprocess;

    int main ()
    {
      // remove earlier existing SHM
      shared_memory_object::remove("SharedMemoryName");

      // create new
      managed_shared_memory segment(create_only,"SharedMemoryName",65536);

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

      //third parameter argument is the ordering function is used to compare the keys.
      typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;

      //offset ptr within SHM for map
      //After construction m_pmap will contain the only object that has been consturcted in pmap[0]
      //If we construct another object it will goto pmap[1]

      offset_ptr<MySHMMap> m_pmap = segment.construct<MySHMMap>("MySHMMapName")(std::less<SharedString>(), alloc_inst);

      //Insert data in the map
      StringAllocator string_alloc_instance( segment.get_segment_manager() );

      SharedString key("case1", string_alloc_instance );
      SharedString inner_map_key("inner_case1", string_alloc_instance );

      SharedString key1("case2", string_alloc_instance );
      SharedString inner_map_key1("inner_case2", string_alloc_instance );

      SharedString inner_map_key2("inner_case3", string_alloc_instance );

      m_pmap[0][key][inner_map_key] = 2030;
      m_pmap[0][key1][inner_map_key1] = 2031;
      m_pmap[0][key][inner_map_key2] = 2034;
      std::cout<<"\n "<<m_pmap[0][key][inner_map_key]<< std::endl;
      std::cout<<"\n "<<m_pmap[0][key1][inner_map_key1]<< std::endl;
      return 0;
    }

reader.cxx

 #include <boost/interprocess/containers/map.hpp>
#include <functional>
#include <utility>
#include <iostream>
#include <ShmTypedefs.h>

using namespace boost::interprocess;

int main ()
{

  try
  {

    managed_shared_memory segment(open_or_create, "SharedMemoryName",65536);

    int size= segment.find<MySHMMap>("MySHMMapName").second;
    std::cout<< "size of map " << size << std::endl;
    offset_ptr<MySHMMap> m_pmap = segment.find<MySHMMap>("MySHMMapName").first;
    MySHMMap :: iterator iter = m_pmap[0].begin();
    MySHMMap :: iterator iter_end = m_pmap[0].end();
    for(; iter != iter_end; ++iter ) {
      std::cout<<"\n "<< iter->first<<std::endl;
      if( iter->first == "case1" ) {
         MappedType::iterator iti = (iter->second).begin();

         MappedType::iterator iti_end = (iter->second).end();
         for( ;iti != iti_end; ++iti ) {
         std::cout << "symbol " << iti->first << " time " << iti->second << std::endl;
          }
      }
    }
  }
  catch(std::exception &e) {
    std::cout<<" error  " << e.what() <<std::endl;
    shared_memory_object::remove("SharedMemoryName");
  }
  return 0;
}

Stack Trace:

Output when I run the program =>

   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)

Core output:

(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

解决方案

Your mapped type:

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

does not allocate its nodes in shared memory. Therefore, the pointers in the outer map are pointing to addresses on the local heap and hence do not exist (or are undefined) on the reading side.

Here is the fixed writer Live On Coliru. Relevant typedefs:

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;

Outputs:

case1, inner_case1, 2030
case1, inner_case2, 2031
case1, inner_case3, 2034

Note:

  • you had a conflicting MySHMMap definition inside main
  • this is very clumsy code.

    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";
    

    Consider wildly simplifying the datastructure and/or abstracting it away in a proper class hiding the uglies

    UPDATE Posted a PoC based on Boost MultiIndex

  • As far as I can tell,

    // 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]
    

    I had never heard of this. Can you point to the documentation where this is stated?

这篇关于试图访问内部地图时程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆