title: boost::interprocess shared memory segment function find() hangs at startup if the segment is already there
body: <p>I'm attempting to use boost's shared memory functionality to put a vector into shared memory, as discussed by <a href="http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/quick_guide.html#interprocess.quick_guide.qg_interprocess_container" rel="nofollow">this boost article</a>.</p>
  
  <p>It mostly all works, except sometimes, when I start up and the shared memory segment is already there, managed_shared_memory::find will hang.</p>
  
  <p>The debugger shows it stuck on some interprocess mutex in the bowels of boost::interprocess.</p>
  
  <p><strong>I've checked and I have no other process that would be running that hangs on to this shared memory.</strong>  </p>
  
  <p>If follow my code, the test starts and if it discovers the segment already existing, it calls destroyMyShm().   destroyMyShm()  opens up the segment and tries to call managed_shared_memory::find() on the vector that should be inside that segment.  This find hangs.</p>
  
  <p>Note if I just call shared_memory_object::remove() everything is ok.  I don't know if this will cause a leak though because the vector won't be properly destroyed.</p>
  
  <p>Is there anything I can do about this?   Possibly a bug in boost?</p>
  
  <p>Is it safe to just call shared_memory_object::remove() and forget about managed_shared_memory::destroy() ?</p>
  
  <pre><code>#include &lt;gtest/gtest.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;boost/interprocess/managed_shared_memory.hpp&gt;
  #include &lt;boost/interprocess/containers/vector.hpp&gt;
  #include &lt;boost/interprocess/allocators/allocator.hpp&gt;
  
  typedef  boost::interprocess::allocator&lt;std::string, boost::interprocess::managed_shared_memory::segment_manager&gt;  ShmemAllocator;
  typedef  boost::interprocess::vector&lt;std::string, ShmemAllocator&gt; MyVector;
  
  
  static const char *kSharedMemorySegmentName("myseg");
  static const char *kSharedMemoryVectorName("myvec");
  
  bool isMyShmCreated();
  void  destroyMyShm();
  void initMyShm(size_t);
  
  //pardon my google-test method here...
  TEST(MyTest, SharedMemoryTest) {
      using namespace boost::interprocess;
      if (isMyShmCreated())
      {
          destroyMyShm();  //hangs in here (see below)
      }
      EXPECT_FALSE(isMyShmCreated());
      initMyShm(1000000);
      EXPECT_TRUE(isMyShmCreated());
  
  
      managed_shared_memory segment(open_only, kSharedMemorySegmentName);
      MyVector *vec = segment.find&lt;MyVector&gt;(kSharedMemoryVectorName).first;
  
      EXPECT_TRUE(vec != 0);
      vec-&gt;push_back(std::string("item 1"));
      vec-&gt;push_back(std::string("item 2"));
  
  
     destroyMyShm();
  }
  
  
  void initMyShm(size_t size) {
      using namespace boost::interprocess;
      if (isMyShmCreated()) {
          log("already created");
      }
      managed_shared_memory segment(open_or_create, kSharedMemorySegmentName, size);
      MyVector *vec = segment.find&lt;MyVector&gt;(kSharedMemoryVectorName).first;
      if (!vec)
      {
          const ShmemAllocator alloc_inst (segment.get_segment_manager());
          segment.construct&lt;MyVector&gt;(kSharedMemoryVectorName)(alloc_inst);
      } else
      {
          vec-&gt;clear();
      }
  }
  
  bool isMyShmCreated()
  {
      using namespace boost::interprocess;
      try
      {
          managed_shared_memory segment(open_only, kSharedMemorySegmentName);
          return segment.check_sanity();
      } catch (const interprocess_exception &amp;ex) {
          std::cout &lt;&lt; "managed_shared_memory ex: "  &lt;&lt; ex.what();
      }
      catch (const std::exception &amp;ex) {
          std::cout &lt;&lt; "managed_shared_memory ex: "  &lt;&lt; ex.what();
      }
      catch (const std::string&amp; ex)
      {
          std::cout &lt;&lt; "managed_shared_memory ex: "  &lt;&lt; ex;
      } catch (...)
      {
          std::cout &lt;&lt; "managed_shared_memory ex: ?";
      }
      return false;
  }
  
  void destroyMyShm() {
      using namespace boost::interprocess;
      try
      {
          managed_shared_memory segment(open_only, kSharedMemorySegmentName);
          // hangs on segment.find() below:
          if (segment.find&lt;MyVector&gt;(kSharedMemoryVectorName).first)  
              segment.destroy&lt;MyVector&gt;(kSharedMemoryVectorName);
      } catch (...) {}
      try
      {
          shared_memory_object::remove(kSharedMemorySegmentName);
      } catch (...) {}
  }
  </code></pre>