title: Segfault in minimal example of scoped_allocator_adaptor used with vector of unordered_map
body: <p>I have recently learned about custom allocators in C++11 and am trying to use them in my application. However I am running into a segfault, which can be reproduced by the following minimal example:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;vector&gt;
  #include &lt;unordered_map&gt;
  #include &lt;scoped_allocator&gt;
  
  template&lt;class T&gt; struct custom_allocator {
      typedef T value_type;
      custom_allocator() noexcept {}
      template &lt;class U&gt; custom_allocator (const custom_allocator&lt;U&gt;&amp;) noexcept {}
      T* allocate (std::size_t n) { return static_cast&lt;T*&gt;(::operator new(n*sizeof(T))); }
      void deallocate (T* p, std::size_t n) { ::delete(p); }
      template &lt;typename U&gt; constexpr bool operator==(const custom_allocator&lt;U&gt;&amp;) const { return true; }
      template &lt;typename U&gt; constexpr bool operator!=(const custom_allocator&lt;U&gt;&amp;) const { return false; }
  };
  
  template&lt;class T&gt; using custom_scoped_allocator = std::scoped_allocator_adaptor&lt;custom_allocator&lt;T&gt; &gt;;
  
  typedef std::unordered_map&lt;int, int, std::hash&lt;int&gt;, std::equal_to&lt;int&gt;,
                             custom_scoped_allocator&lt;std::pair&lt;const int, int&gt; &gt; &gt; MyMap;
  
  typedef std::vector&lt;MyMap, custom_scoped_allocator&lt;MyMap&gt; &gt; MyVector;
  
  int main() {
      MyVector vec(1);
      vec[0][0] = 0;
      return 0;
  }
  </code></pre>
  
  <p>The custom allocator is exactly the one suggested in the example at
  <a href="http://www.cplusplus.com/reference/memory/allocator_traits/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/memory/allocator_traits/</a>
  It is then combined with <code>std::scoped_allocator_adaptor</code> as I have seen suggested in a few places on the internet.</p>
  
  <p>The code compiles fine with gcc 5.4.0 using <code>g++ -g3 -O0 -std=c++11 -march=core-avx-i -o tmpalloc tmpalloc.cpp</code>. When I attempt to run it, a segfault is reported starting with</p>
  
  <pre><code>*** Error in `./tmpalloc': double free or corruption (fasttop): 0x0000000000ae3c90 ***
  </code></pre>
  
  <p>If I recompile using AddressSanitizer, I get the following details about the double free: <a href="https://pastebin.com/raw/xy2NQtD0" rel="nofollow noreferrer">https://pastebin.com/raw/xy2NQtD0</a>
  (Sorry for the pastebin but it is not pretty.)
  Basically, the destructor for an unordered_map object seems to be called twice.</p>
  
  <p>What am I doing wrong?</p>