title: gSOAP multithreaded server crashes when trying to deallocate memory
body: <p>I have created a standalone multithreaded webservice using gSOAP framework but this code has memory leak and when i try to deallocte that memory it crashes after sometime.</p>
  <p>Below is the detailed steps i have followed for creating webservice.</p>
  <ol>
  <li>soapcpp2 -i -S -I Services.h</li>
  <li>compiled the code by adding the generated files and stdsoap.h/cpp files in visual studio.</li>
  </ol>
  <p>Below is the code where i am spawning whenever i receive a new request.</p>
  <pre><code>struct Services
  {
      
          abcService* aServver;
          defService* dServer;
          ghiService* gServer;
          jklService* jServer;
  }
  
  DWORD WINAPI StartThread(void* lparam)`
  {
          
          Services *sp = static_cast&lt;Services*&gt;(lparam);
  
          if(soap_begin_serve(sp-&gt;aServer-&gt;soap)){
              soap_send_fault(sp-&gt;aServer);
          }
          else if(sp-&gt;aServer-&gt;dispatch() == SOAP_NO_METHOD)
          {
              soap_copy_stream(sp-&gt;dServer,sp-&gt;aServer);
              soap_free_stream(sp-&gt;aServer);
              if(sp-&gt;dServer-&gt;dispatch() == SOAP_NO_METHOD)
              {
                  soap_copy_stream(sp-&gt;gServer,sp-&gt;dServer);
                  soap_free_stream(sp-&gt;dServer);
                  if(sp-&gt;gServer-&gt;dispatch() == SOAP_NO_METHOD)
                  {
                      soap_copy_stream(sp-&gt;jServer,sp-&gt;gServer);
                      soap_free_stream(sp-&gt;gServer);
  
                      if(sp-&gt;jServer-&gt;dispatch())
                      {
                          soap_send_fault(sp-&gt;jServer);
                      }
                     sp-&gt;jServer-&gt;destroy();
                  }
              sp-&gt;gServer-&gt;destroy();
           }
          sp-&gt;dServer-&gt;destroy();
      }
      sp-&gt;aServer-&gt;destroy();
      delete(sp-&gt;aServer);    //---&gt;Line 48
      delete(sp-&gt;dSever);     //---&gt;Line 49
      delete(sp-&gt;gServer);    //---&gt;Line 50
      delete(sp-&gt;jServer);    //---&gt;Line 51
      return 0;
  }
  
  int main()
  {
      abcService aServver;
      defService dServer;
      ghiService gServer;
      jklService jServer;
  
      aServer.bind(NULL,8090,100);
      Services servv;
      for(;;)
      {
              aServer.accept();
  
              
              servv.aServver = aServer.copy();   //---&gt;Line no 21
              servv.dServer = dServer.copy();    //---&gt;Line no 22
              servv.gServer = gServer.copy();    //---&gt;Line no 23
              servv.jServer = jServer.copy();    //---&gt;Line no 24
  
              DWORD Threadid;
              HANDLE handle = CreateThread(NULL,0,StartThread,(void*)&amp;servv,0,&amp;Threadid);
              CloseHandle(handle);
       }
  }
  
  </code></pre>
  <p>Memory is created starting from line no 21 to Line 24 and these objects are passed to thread spawned on each request.</p>
  <p>Once the request is processed in the spawned thread, then i try to delete these objects starting at line no 48.
  After sometime , it starts crashing randomly at line no 48. This issue is reproducible.</p>
  <p>I am not sure what is causing this issue , can anybody help me here ?</p>