Xerces-C ++ XML处理程序尽管仅在本地名称上调用转码,将其打印然后释放,但仍存在内存错误[已解决]

问题描述

我在Xerces-C ++中使用自定义XML处理程序遇到内存错误问题。运行valgrind的建议是,尽管我释放了转码输出,但它们还是来自转码函数本身。

我决定尝试使用Xerces-C ++ SAX2网页上最基本的自定义处理程序: https://xerces.apache.org/xerces-c/program-sax2-3.html

调整代码后,它看起来像这样:

//From main function:
        string xmlFile = "./xmlfiles/myFile.xml";
        SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
        parser->setFeature(XMLUni::fgSAX2CoreValidation,true);
        parser->setFeature(XMLUni::fgSAX2CoreNameSpaces,true);   // optional

        DefaultHandler* defaultHandler = new MySAX2Handler();
        parser->setContentHandler(defaultHandler);
        parser->setErrorHandler(defaultHandler);

        try {
            parser->parse(xmlFile.c_str());
        }


MySAX2Handler::MySAX2Handler()
{
}

void MySAX2Handler::startElement(const   XMLCh* const    uri,const   XMLCh* const    localname,const   XMLCh* const    qname,const   xercesc::Attributes&     attrs)
{
    char* message = xercesc::XMLString::transcode(localname);
    cout << "I saw element: "<< message << endl;
    xercesc::XMLString::release(&message);
}

void MySAX2Handler::fatalError(const xercesc::SAXParseException& exception)
{
    char* message = xercesc::XMLString::transcode(exception.getMessage());
    cout << "Fatal error: " << message
         << " at line: " << exception.getLineNumber()
         << endl;
    xercesc::XMLString::release(&message);
}

但是编译它仍然会遇到内存错误

==19255== Conditional jump or move depends on uninitialised value(s)
==19255==    at 0x5A92107: __wcsnlen_avx2 (strlen-avx2.S:275)
==19255==    by 0x59BFF31: wcsrtombs (wcsrtombs.c:104)
==19255==    by 0x5945BD0: wcstombs (wcstombs.c:34)
==19255==    by 0x503E16E: xercesc_2_8::IconvLCPTranscoder::transcode(unsigned short const*,xercesc_2_8::MemoryManager*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x50541FA: xercesc_2_8::XMLPlatformUtils::openFile(unsigned short const*,xercesc_2_8::MemoryManager*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x4FBF75A: xercesc_2_8::BinFileInputStream::BinFileInputStream(unsigned short const*,xercesc_2_8::MemoryManager*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x5056565: xercesc_2_8::LocalFileInputSource::makeStream() const (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x5070C89: xercesc_2_8::ReaderMgr::createReader(xercesc_2_8::InputSource const&,bool,xercesc_2_8::XMLReader::RefFrom,xercesc_2_8::XMLReader::Types,xercesc_2_8::XMLReader::Sources,bool) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x504438E: xercesc_2_8::IGXMLScanner::scanReset(xercesc_2_8::InputSource const&) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x504F5F2: xercesc_2_8::IGXMLScanner::scanDocument(xercesc_2_8::InputSource const&) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x5104925: xercesc_2_8::XMLScanner::scanDocument(unsigned short const*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x51068C7: xercesc_2_8::XMLScanner::scanDocument(char const*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==  Uninitialised value was created by a stack allocation
==19255==    at 0x503E09E: xercesc_2_8::IconvLCPTranscoder::transcode(unsigned short const*,xercesc_2_8::MemoryManager*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255== 
I saw element: Apples
I saw element: Apple
I saw element: Variety
==19255== Conditional jump or move depends on uninitialised value(s)
==19255==    at 0x5A920E7: __wcsnlen_avx2 (strlen-avx2.S:261)
==19255==    by 0x59BFF31: wcsrtombs (wcsrtombs.c:104)
==19255==    by 0x5945BD0: wcstombs (wcstombs.c:34)
==19255==    by 0x503E040: xercesc_2_8::IconvLCPTranscoder::transcode(unsigned short const*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x10E060: MySAX2Handler::startElement(unsigned short const*,unsigned short const*,xercesc_2_8::Attributes const&) (MySAX2Handler.cpp:15)
==19255==    by 0x5082D20: xercesc_2_8::SAX2XMLReaderImpl::startElement(xercesc_2_8::XMLElementDecl const&,unsigned int,xercesc_2_8::RefVectorOf<xercesc_2_8::XMLAttr> const&,bool) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x504C0B4: xercesc_2_8::IGXMLScanner::scanStartTagNS(bool&) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x504F51C: xercesc_2_8::IGXMLScanner::scanContent() (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x504F621: xercesc_2_8::IGXMLScanner::scanDocument(xercesc_2_8::InputSource const&) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x5104925: xercesc_2_8::XMLScanner::scanDocument(unsigned short const*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x51068C7: xercesc_2_8::XMLScanner::scanDocument(char const*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==    by 0x507F95E: xercesc_2_8::SAX2XMLReaderImpl::parse(char const*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255==  Uninitialised value was created by a stack allocation
==19255==    at 0x503DF76: xercesc_2_8::IconvLCPTranscoder::transcode(unsigned short const*) (in /home/myName/xerces-c_2_8_0-x86_64-linux-gcc_3_4/lib/libxerces-c.so.28.0)
==19255== 

所以看来处理程序中的任何代码都没有发生错误

我错过了什么吗?难道我搞砸了Xerces-C ++安装吗?我尝试从源代码安装,但是遇到了问题,因此我下载了2.8版二进制文件。我之所以提出这一点,是因为我不知道这段代码还会引起内存问题。

编辑:因此,我重新安装了Xerces-C ++,确保使用最新版本。以某种方式解决了一些错误。其余的内存泄漏已按以下说明清除:https://xerces.apache.org/xerces-c/faq-parse-3.html#faq-7

Xerces-C ++库分配和缓存一些经常重复使用的项目。某些堆分析工具可能会将这些存储区报告为内存泄漏;为避免此问题,请在应用程序退出之前调用XMLPlatformUtils :: Terminate()函数

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)