1

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

1 回答 1

1

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

m_OptionA = XMLString::transcode(xmlch_OptionA);

文档中我们可以看到,转码要求它的调用者用XMLString::release(). 我们可以看到这是在GetConfig析构函数中完成的:

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

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

于 2011-01-17T19:47:46.987 回答