title: boost::interprocess::managed_shared_memory crashes program
body: <p>My goal is to create a template singleton class called SharedMemory that can store a given data structure in a map in shared memory using boost::interprocess::managed_shared_memory.</p>
  
  <pre><code>#ifndef SHARED_MEMORY_H_
  #define SHARED_MEMORY_H_
  
  #include &lt;boost/interprocess/managed_shared_memory.hpp&gt;
  #include &lt;boost/interprocess/containers/map.hpp&gt;
  #include &lt;boost/interprocess/allocators/allocator.hpp&gt;
  #include &lt;functional&gt;
  #include &lt;string&gt;
  #include &lt;utility&gt;
  #include &lt;map&gt;
  
  using namespace boost::interprocess;
  
  template&lt;typename T&gt;
  class SharedMemory {
    typedef std::pair&lt;std::string, T&gt; ValueType;
    typedef allocator&lt;ValueType, managed_shared_memory::segment_manager&gt;
      ShmemAllocator;
    typedef map&lt;std::string, T, std::less&lt;std::string&gt;, ShmemAllocator&gt; SharedMap;
  
    public:
      static SharedMemory&lt;T&gt;&amp; instance();
      void Create();
      void Destory();
      void insert(T* t);
      std::map&lt;std::string, T&gt; getMapOfRecords();
    private:
    SharedMemory();
    ~SharedMemory(){delete m_segment;}
    void Initialize();
    managed_shared_memory* m_segment;
    std::size_t m_size;
  };
  
  template&lt;typename T&gt;
  inline void SharedMemory&lt;T&gt;::Create() {
    Destory();
    m_segment = new  managed_shared_memory(create_only, T::memory(), m_size);
    ShmemAllocator alloc_inst (m_segment-&gt;get_segment_manager());
    m_segment-&gt;construct&lt;SharedMap&gt;("SageMap")(std::less&lt;std::string&gt;(), alloc_inst);
  }
  
  template&lt;typename T&gt;
  inline void SharedMemory&lt;T&gt;::Destory() {
    shared_memory_object::remove(T::memory());
  }
  
  template&lt;typename T&gt;
  inline SharedMemory&lt;T&gt;&amp; SharedMemory&lt;T&gt;::instance() {
    static SharedMemory&lt;T&gt; instance;
    return instance;
  }
  
  template&lt;typename T&gt;
  inline SharedMemory&lt;T&gt;::SharedMemory()
      : m_size(65536) {
  
  }
  
  template&lt;typename T&gt;
  inline void SharedMemory&lt;T&gt;::insert(T* t) {
    SharedMap* mymap = m_segment-&gt;find&lt;SharedMap&gt;("SageMap").first;
    mymap-&gt;insert(std::pair&lt;std::string, T&gt;(t-&gt;key(), *t));
  }
  
  template&lt;typename T&gt;
  inline std::map&lt;std::string, T&gt; SharedMemory&lt;T&gt;::getMapOfRecords() {
    SharedMap* mymap = m_segment-&gt;find&lt;SharedMap&gt;("SageMap").first;
    return std::map&lt;std::string, T&gt;(mymap-&gt;begin(), mymap-&gt;end());
  }
  #endif
  </code></pre>
  
  <p>Here is an example of how its used.</p>
  
  <pre><code>#include &lt;boost/lexical_cast.hpp&gt;
  #include &lt;cstring&gt;
  #include &lt;cstdlib&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;functional&gt;
  #include &lt;utility&gt;
  #include "SharedMemory.hpp"
  
  struct simple_type {
    int i;
    std::string key() {return boost::lexical_cast&lt;std::string&gt;(i);}
    static const char* memory() {return std::string("simple_memory_page").c_str();}
    simple_type(int i): i(i){}
  };
  
  int main(int argc, char *argv[])
  {
    if(argc == 1) {
      SharedMemory&lt;simple_type&gt;&amp; test = SharedMemory&lt;simple_type&gt;::instance();
      test.Create();
      test.insert(new simple_type(1));
      test.insert(new simple_type(2));
      std::string s(argv[0]); s += " child ";
      if(0 != std::system(s.c_str()))
          return 1;
      test.Destory();
    } else {
      SharedMemory&lt;simple_type&gt;&amp; test = SharedMemory&lt;simple_type&gt;::instance();
      std::map&lt;std::string, simple_type&gt; records = test.getMapOfRecords();
      for(auto it = records.begin(); it != records.end(); ++it) {
        std::cout &lt;&lt; it-&gt;second.i &lt;&lt; std::endl;
      }
    }
    return 0;
  }
  </code></pre>
  
  <p>Here is a stack trace:</p>
  
  <pre><code>position_monitor_eu.exe!boost::interprocess::offset_ptr&lt;boost::intrusive::compact_rbtree_node&lt;boost::interprocess::offset_ptr&lt;void&gt; &gt; &gt;::get_pointer()  Line 81 + 0x1a bytes    C++
  position_monitor_eu.exe!boost::interprocess::offset_ptr&lt;boost::intrusive::compact_rbtree_node&lt;boost::interprocess::offset_ptr&lt;void&gt; &gt; &gt;::get()  Line 153 + 0x16 bytes   C++
  position_monitor_eu.exe!boost::interprocess::offset_ptr&lt;boost::intrusive::compact_rbtree_node&lt;boost::interprocess::offset_ptr&lt;void&gt; &gt; &gt;::offset_ptr&lt;boost::intrusive::compact_rbtree_node&lt;boost::interprocess::offset_ptr&lt;void&gt; &gt; &gt;(const boost::interprocess::offset_ptr&lt;boost::intrusive::compact_rbtree_node&lt;boost::interprocess::offset_ptr&lt;void&gt; &gt; &gt; &amp; ptr={...})  Line 117 + 0x16 bytes   C++
  position_monitor_eu.exe!boost::intrusive::compact_rbtree_node_traits_impl&lt;boost::interprocess::offset_ptr&lt;void&gt; &gt;::get_left(boost::interprocess::offset_ptr&lt;boost::intrusive::compact_rbtree_node&lt;boost::interprocess::offset_ptr&lt;void&gt; &gt; const &gt; n={...})  Line 124 + 0x17 bytes   C++
  position_monitor_eu.exe!boost::intrusive::rbtree_impl&lt;boost::intrusive::setopt&lt;boost::intrusive::detail::base_hook_traits&lt;boost::container::containers_detail::rbtree_node&lt;std::pair&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;,simple_type&gt;,boost::interprocess::offset_ptr&lt;void&gt; &gt;,boost::intrusive::rbtree_node_traits&lt;boost::interprocess::offset_ptr&lt;void&gt;,1&gt;,0,boost::intrusive::default_tag,3&gt;,boost::container::containers_detail::node_compare&lt;boost::container::containers_detail::value_compare_impl&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;,std::pair&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; const ,simple_type&gt;,std::less&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; &gt;,boost::container::containers_detail::select1st&lt;std::pair&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; const ,simple_type&gt; &gt; &gt;,boost::container::containers_detail::rbtree_node&lt;std::pair&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;,simple_type&gt;,boost::interprocess::offset_ptr&lt;void&gt; &gt; &gt;,unsigned int,1&gt; &gt;::begin()  Line 273 + 0x42 bytes   C++
  position_monitor_eu.exe!boost::container::containers_detail::rbtree&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;,std::pair&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; const ,simple_type&gt;,boost::container::containers_detail::select1st&lt;std::pair&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; const ,simple_type&gt; &gt;,std::less&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; &gt;,boost::interprocess::allocator&lt;std::pair&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;,simple_type&gt;,boost::interprocess::segment_manager&lt;char,boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family,boost::interprocess::offset_ptr&lt;void&gt;,0&gt;,boost::interprocess::iset_index&gt; &gt; &gt;::begin()  Line 493 + 0x28 bytes  C++
  position_monitor_eu.exe!boost::container::map&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;,simple_type,std::less&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; &gt;,boost::interprocess::allocator&lt;std::pair&lt;std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;,simple_type&gt;,boost::interprocess::segment_manager&lt;char,boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family,boost::interprocess::offset_ptr&lt;void&gt;,0&gt;,boost::interprocess::iset_index&gt; &gt; &gt;::begin()  Line 245 + 0x1a bytes   C++
  position_monitor_eu.exe!SharedMemory&lt;simple_type&gt;::getMapOfRecords()  Line 68 + 0x1e bytes  C++
  position_monitor_eu.exe!main(int argc=2, char * * argv=0x02b03db8)  Line 200 + 0xc bytes    C++
  position_monitor_eu.exe!__tmainCRTStartup()  Line 555 + 0x19 bytes  C
  position_monitor_eu.exe!mainCRTStartup()  Line 371  C
  kernel32.dll!77003677()     
  [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
  ntdll.dll!775cc002()    
  ntdll.dll!775cbfd5()    
  </code></pre>
  
  <p>My current issue is the program crashes to calls to <code>mymap-&gt;begin()</code> in <code>getMapOfRecords()</code>.</p>