title: Just leaking? Or undefined behavior?
body: <p>In an old implementation of a connection pool, I found this beauty (strongly simplified):</p>
  
  <pre><code>#include &lt;memory&gt;
  #include &lt;queue&gt;
  
  struct Connection
  {
  };
  
  auto pool = std::queue&lt;std::shared_ptr&lt;Connection&gt;&gt;{};
  
  auto pushConnection(Connection* connection) -&gt; void
  {
    // This uses pushConnection as custom deleter in the shared_ptr
    pool.emplace(connection, pushConnection);
  }
  
  int main()
  {
    pushConnection(new Connection{});
  }
  </code></pre>
  
  <p>The original idea was that you can obtain connections from the pool and once you are done, the custom deleter (<code>pushConnection</code>) would automatically return it to the pool.</p>
  
  <p>However, storing the connections in the pool with the same custom deleter is more than just a little weird.</p>
  
  <p>At bare minimum, there is a resource leak: When the pool goes out of scope at the end of the program, the destructor of <code>queue</code> will be called. This will destroy the <code>shared_ptr</code>s, which will call their custom deleter, which will emplace the connection back into the queue.</p>
  
  <p>I wonder, if it is also undefined behavior to <code>emplace</code> into a <code>queue</code> that is currently in the process of being destroyed?</p>