libxml2 crash on second use on Windows

空扰寡人 提交于 2019-12-13 06:41:37

问题


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


回答1:


Are the two calls in different threads?

Have you called the xmlInitParser function to initialize the library. A missing call to xmlInitParser will produce a call stack like yours in multi-threaded applications.



来源:https://stackoverflow.com/questions/3127472/libxml2-crash-on-second-use-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!