2011-01-17 3 views
1

xerces-c를 배우려고하지 않고 온라인에서이 자습서를 진행했습니다. xerces-c : 여러 파일을 구문 분석하는 Xml

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

내가 컴파일하고 나는 약간 프로그램에 변경을 만든 경우에는 아무런 문제가있는 메모리 검사기 (Valgrind의)를 통해 실행할 수있는 튜토리얼을 얻을 수 있었다, 메모리 검사는 몇 가지 잠재적 인 누출 바이트를 반환했습니다. 나는 프로그램이 하나가 아닌 두 개의 파일을 읽을 수 있도록 메인에 몇 줄을 추가했다. 그것을 왜 내가 다른 XML 파일에서 읽을 코드의 여분의 라인을 추가 할 때

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; 
} 

궁금 해서요, 그것은 다음과 같은 출력을 초래할 것인가?

==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

예제 코드에는 사용 사례에 대한 몇 가지 단점이 있습니다. 우리가 트랜스 코딩을 볼 수 있습니다 the documentation에서

m_OptionA = XMLString::transcode(xmlch_OptionA); 

XMLString::release()으로 반환 된 (C-스타일) 문자열의 할당을 해제하는 호출자가 필요합니다 : 그것은이 코드가 포함되어 있습니다. 우리는이 GetConfig 소멸자에서 수행되는 것을 볼 수 있습니다

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

하지만이 코드는 readConfig()에 존재하지 않습니다. 거기에 추가해야합니다. C 스타일의 문자열 멤버를 생성자에서 NULL로 초기화하거나, readConfig()을 1이나 2 대신 0 번 호출하면 다른 메모리 문제 (잠재적으로 충돌 버그)가 발생할 수 있습니다.

+0

아아 나는 readConfig() 함수에 릴리스하려고했는데 완벽하게 작동했습니다. 유일한 문제는 멤버 변수가 릴리스되기 전에 print 문을 호출해야한다는 것이 었습니다. 감사. – user459811

+0

좋습니다. 아마도 당신은 그 때 받아 들여지는 나의 응답을 표시하기 위하여 아주 친절 할 것입니다. –

관련 문제