xerces-c:解析多个文件的Xml

时间:2011-01-17 19:39:15

标签: c++ xml xerces

我正在尝试学习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)

1 个答案:

答案 0 :(得分:1)

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

m_OptionA = XMLString::transcode(xmlch_OptionA);

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

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

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