views:

83

answers:

1

Hi,

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:

xmlSAXHandler saxHandler;
memset(&saxHandler, 0, sizeof(m_SaxHandler));
xmlSAXVersion(&saxHandler, 2);
saxHandler.initialized = XML_SAX2_MAGIC;  // so we do this to force parsing as SAX2.
saxHandler.startElementNs = &startElementNs;
saxHandler.endElementNs = &endElementNs;
saxHandler.warning = &warning;
saxHandler.error = &error;
saxHandler.characters = &characters;

xmlParserCtxtPtr pSaxCtx = xmlCreatePushParserCtxt(&m_SaxHandler, this, 0, 0, 0);

I then feed in the XML stream using xmlParseChunk() and use the callbacks to process the data, once parsing is complete, I call xmlFreeParserCtxt(pSaxCtx) 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:

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

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.

Any ideas?

Thanks, J

A: 

I am not a C++ coder, but anyway the following link might be of interest: http://0pointer.de/blog/projects/beware-of-xmlCleanupParser.html

Thanks but it's not that, I do call `xmlCleanupParser()` but only once and just before exit as recommended in that article.