首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xerces-c: Xml解析多个文件

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

Stack Overflow用户
提问于 2011-01-18 03:39:15
回答 1查看 1.4K关注 0票数 1

我正在努力学习xerces-c,并在线学习本教程。

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

我能够让教程编译并运行一个内存检查器(valgrind),没有任何问题,但是当我稍微修改程序时,内存检查器返回了一些潜在的泄漏字节。我只在main中添加了一些额外的行,以允许程序读取两个文件而不是一个文件。

代码语言:javascript
复制
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文件时,结果会是下面的输出?

代码语言:javascript
复制
==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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-18 03:47:47

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

代码语言:javascript
复制
m_OptionA = XMLString::transcode(xmlch_OptionA);

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

代码语言:javascript
复制
if(m_OptionA)   XMLString::release( &m_OptionA );

但是这个代码在readConfig()中并不存在。您应该将其添加到那里。您可能还希望在构造函数中将这些C样式的字符串成员初始化为NULL,否则,如果您调用readConfig() 0次而不是1到2次,您将面临另一个内存问题(可能是崩溃错误)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4717089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档