title: C++ Map Iteration and Stack Corruption
body: <p>I am trying to use a system of maps to store and update data for a chat server. The application is mutlithreaded and uses a lock system to prevent multiple threads from accessing the data. </p>
  
  <p>The problem is this: when a client is removed individually from the map, it is ok. However, when I try to call multiple closes, it leaves some in the memory. If I at any point call ::clear() on the map, it causes a debug assertion error with either "Iterator not compatible" or similar. The code will work the first time (tested using 80+ consoles connected as a test), but due to it leaving chunks behind, will not work again. I have tried researching ways, and I have written systems to stop the code execution until each process has completed. I appreciate any help so far, and I have attached the relevant code snippets.</p>
  
  <pre><code>//portion of server code that handles shutting down
  DWORD WINAPI runserver(void *params) {  
      runserverPARAMS *p = (runserverPARAMS*)params;  
      /*Server stuff*/                            
  
      serverquit = 0; 
      //client based cleanup
      vector&lt;int&gt; tokill;
      map&lt;int,int&gt;::iterator it = clientsockets.begin();
  
      while(it != clientsockets.end()) {      
          tokill.push_back(it-&gt;first);
          ++it;
      }
      for(;;) {
          for each (int x in tokill) {
              clientquit[x] = 1;
              while(clientoffline[x] != 1) {
                  //haulting execution until thread has terminated
              }
              destoryclient(x);
          }
      }
      //client thread based cleanup complete.
      return 0;
  }
  
  
  //clientioprelim
  DWORD WINAPI clientioprelim(void* params) {
      CLIENTthreadparams *inparams = (CLIENTthreadparams *)params;
      /*Socket stuff*/
      for(;;) {       
          /**/
          }
          else {
              if(clientquit[inparams-&gt;clientid] == 1)
                  break;
          }
      }
      clientoffline[inparams-&gt;clientid] = 1;
      return 0;
  }
  
  int LOCKED; //exported as extern via libraries.h so it's visible to other source files
  
  void destoryclient(int clientid) {
      for(;;) {
          if(LOCKED == 0) {
              LOCKED = 1;         
              shutdown(clientsockets[clientid], 2);
              closesocket(clientsockets[clientid]);
              if((clientsockets.count(clientid) != 0) &amp;&amp; (clientsockets.find(clientid) != clientsockets.end()))
                  clientsockets.erase(clientsockets.find(clientid));                  
              if((clientname.count(clientid) != 0) &amp;&amp; (clientname.find(clientid) != clientname.end()))
                  clientname.erase(clientname.find(clientid));
              if((clientusername.count(clientid) != 0) &amp;&amp; (clientusername.find(clientid) != clientusername.end()))
                  clientusername.erase(clientusername.find(clientid));
              if((clientaddr.count(clientid) != 0) &amp;&amp; (clientaddr.find(clientid) != clientaddr.end()))
                  clientaddr.erase(clientusername.find(clientid));
              if((clientcontacts.count(clientid) != 0) &amp;&amp; (clientcontacts.find(clientid) != clientcontacts.end())) 
                  clientcontacts.erase(clientcontacts.find(clientid));
              if((clientquit.count(clientid) != 0) &amp;&amp; (clientquit.find(clientid) != clientquit.end()))
                  clientquit.erase(clientquit.find(clientid));    
              if((clientthreads.count(clientid) != 0) &amp;&amp; (clientthreads.find(clientid) != clientthreads.end())) 
                  clientthreads.erase(clientthreads.find(clientid));
              LOCKED = 0;
              break;          
          }
      }
      return;
  }
  </code></pre>