【问题标题】:xerces-c: Xml parsing multiple filesxerces -c:解析多个文件的xml
【发布时间】:2011-01-17 19:39:15
【问题描述】:

我正在尝试学习 xerces-c 并在线学习本教程。

http://www.yolinux.com/TUTORIALS/XML-Xerces-C.html

我能够通过内存检查器 (valgrind) 毫无问题地编译和运行本教程,但是当我对程序稍作更改时,内存检查器返回了一些潜在的泄漏字节。我只在 main 中添加了几行额外的行,以允许程序读取两个文件而不是一个。

int main()
{
   string configFile="sample.xml"; // stat file. Get ambigious segfault otherwise.

   GetConfig appConfig;

   appConfig.readConfigFile(configFile);

   cout << "Application option A="  << appConfig.getOptionA()  << endl;
   cout << "Application option B="  << appConfig.getOptionB()  << endl;

   // Added code
   configFile = "sample1.xml";
   appConfig.readConfigFile(configFile);

   cout << "Application option A="  << appConfig.getOptionA()  << endl;
   cout << "Application option B="  << appConfig.getOptionB()  << endl;

   return 0;
}

我想知道为什么当我添加额外的代码行以读取另一个 xml 文件时,会导致以下输出?

==776== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==776== Command: ./a.out
==776== 
Application option A=10
Application option B=24
Application option A=30
Application option B=40
==776== 
==776== HEAP SUMMARY:
==776==     in use at exit: 6 bytes in 2 blocks
==776==   total heap usage: 4,031 allocs, 4,029 frees, 1,092,045 bytes allocated
==776== 
==776== 3 bytes in 1 blocks are definitely lost in loss record 1 of 2
==776==    at 0x4C28B8C: operator new(unsigned long) (vg_replace_malloc.c:261)
==776==    by 0x5225E9B: xercesc_3_1::MemoryManagerImpl::allocate(unsigned long) (MemoryManagerImpl.cpp:40)
==776==    by 0x53006C8: xercesc_3_1::IconvGNULCPTranscoder::transcode(unsigned short const*, xercesc_3_1::MemoryManager*) (IconvGNUTransService.cpp:751)
==776==    by 0x4038E7: GetConfig::readConfigFile(std::string&) (in /home/bonniehan/workspace/test/a.out)
==776==    by 0x403B13: main (in /home/bonniehan/workspace/test/a.out)
==776== 
==776== 3 bytes in 1 blocks are definitely lost in loss record 2 of 2
==776==    at 0x4C28B8C: operator new(unsigned long) (vg_replace_malloc.c:261)
==776==    by 0x5225E9B: xercesc_3_1::MemoryManagerImpl::allocate(unsigned long) (MemoryManagerImpl.cpp:40)
==776==    by 0x53006C8: xercesc_3_1::IconvGNULCPTranscoder::transcode(unsigned short const*, xercesc_3_1::MemoryManager*) (IconvGNUTransService.cpp:751)
==776==    by 0x40393F: GetConfig::readConfigFile(std::string&) (in /home/bonniehan/workspace/test/a.out)
==776==    by 0x403B13: main (in /home/bonniehan/workspace/test/a.out)
==776== 
==776== LEAK SUMMARY:
==776==    definitely lost: 6 bytes in 2 blocks
==776==    indirectly lost: 0 bytes in 0 blocks
==776==      possibly lost: 0 bytes in 0 blocks
==776==    still reachable: 0 bytes in 0 blocks
==776==         suppressed: 0 bytes in 0 blocks
==776== 
==776== For counts of detected and suppressed errors, rerun with: -v
==776== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)

【问题讨论】:

    标签: c++ xml xerces


    【解决方案1】:

    看起来示例代码对您的用例有一些缺点。它包含以下代码:

    m_OptionA = XMLString::transcode(xmlch_OptionA);
    

    the documentation 我们可以看到转码要求其调用者使用XMLString::release() 释放返回的(C 风格)字符串。我们可以看到这是在GetConfig析构函数中完成的:

    if(m_OptionA)   XMLString::release( &m_OptionA );
    

    readConfig() 中不存在此代码。你应该在那里添加它。您可能还想在构造函数中将这些 C 风格的字符串成员初始化为 NULL,否则如果您调用 readConfig() 零次而不是一两次,您将面临另一个内存问题(可能是崩溃错误)。

    【讨论】:

    • 啊,我尝试将发行版放入 readConfig() 函数中,并且效果很好。唯一的问题是我需要在释放成员变量之前调用打印语句。谢谢。
    猜你喜欢
    • 2013-07-22
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多