title: C++ remove std::list element in loop issue
body: <p>Good day. I want to understand, why this works good:</p>
  
  <pre><code>    std::list&lt;Contact&gt;::iterator it = contacts.begin();
  
      while (it != contacts.end()) {
          if ((*it).status == Contact::Status::disconnected) {
              (*it).handler.detach();
              it = contacts.erase(it);
          }
          else {
              it++;
          }   
      }
  </code></pre>
  
  <p>but this causes crash with message "abort() has been called":</p>
  
  <pre><code>    contacts.remove_if([](Contact c) {
          if (c.status != Contact::Status::disconnected)
              return false;
  
          c.handler.detach();
  
          return true;
      });
  </code></pre>
  
  <p>All this executes in separated thread inside critical section. List and critical section declared globally as:</p>
  
  <pre><code>CRITICAL_SECTION criticalSection;
  std::list&lt;Contact&gt; contacts;
  </code></pre>