title: libxml2 crash on second use on Windows
body: <p>I've been using libxml2 push parsing (SAX) to parse an incoming XML stream, this works well first time but crashes on the second attempt every time, my code looks like this:</p>
  
  <pre><code>xmlSAXHandler saxHandler;
  memset(&amp;saxHandler, 0, sizeof(m_SaxHandler));
  xmlSAXVersion(&amp;saxHandler, 2);
  saxHandler.initialized = XML_SAX2_MAGIC;  // so we do this to force parsing as SAX2.
  saxHandler.startElementNs = &amp;startElementNs;
  saxHandler.endElementNs = &amp;endElementNs;
  saxHandler.warning = &amp;warning;
  saxHandler.error = &amp;error;
  saxHandler.characters = &amp;characters;
  
  xmlParserCtxtPtr pSaxCtx = xmlCreatePushParserCtxt(&amp;m_SaxHandler, this, 0, 0, 0);
  </code></pre>
  
  <p>I then feed in the XML stream using <code>xmlParseChunk()</code> and use the callbacks to process the data, once parsing is complete, I call <code>xmlFreeParserCtxt(pSaxCtx)</code> to free the context. As I mentioned, this all works perfectly on the first set of data but when the code is run again I get an access violation, the stack trace is:</p>
  
  <pre><code>ntdll.dll!_RtlpWaitOnCriticalSection@8()  + 0x99 bytes 
  ntdll.dll!_RtlEnterCriticalSection@4()  + 0x168d8 bytes 
  ntdll.dll!_RtlpWaitOnCriticalSection@8()  + 0x99 bytes  
  ntdll.dll!_RtlEnterCriticalSection@4()  + 0x168d8 bytes 
  libxml2.dll!xmlGetGlobalState()  Line 716   C
  libxml2.dll!__xmlDefaultBufferSize()  Line 814 + 0x5 bytes  C
  libxml2.dll!xmlAllocParserInputBuffer(xmlCharEncoding enc)  Line 2281 + 0x5 bytes   C
  libxml2.dll!xmlCreatePushParserCtxt(_xmlSAXHandler * sax, void * user_data, const char * chunk, int size, const char * filename)  Line 11695 + 0x9 bytes    C
  TestApp.exe!XMLProcessor::XMLProcessor(const wchar_t * szHost=0x00d3d80c, const wchar_t * szUri=0x00d3db40, bool secure=false)  Line 16 + 0x19 bytes C++
  TestApp.exe!WorkerThread::ThreadProc(void * lpParameter=0x00a351c0)  Line 34 + 0x15 bytes C++
  kernel32.dll!@BaseThreadInitThunk@12()  + 0x12 bytes 
  ntdll.dll!___RtlUserThreadStart@8()  + 0x27 bytes 
  ntdll.dll!__RtlUserThreadStart@8()  + 0x1b bytes
  </code></pre>
  
  <p>It seems to be trying to lock a critical section which is either non-existant or corrupted but I cannot figure how/why it works first time and not second.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks,
  J</p>