누출. 나는 그것이 뭔가를 가지고 있다고 생각의 getline() Valgrind의 메모리는 내가의 getline와 메모리 누수를 얻고, 내가 왜하거나 중지하는 방법을 잘 모르겠습니다
string line;
bool pending = getline(inputfile, line);
int round = readOption(inputfile);
int num = readOption(inputfile);
:
여기==26681==
==26681== HEAP SUMMARY:
==26681== in use at exit: 1,756 bytes in 73 blocks
==26681== total heap usage: 223 allocs, 150 frees, 15,523 bytes allocated
==26681==
==26681== 28 bytes in 1 blocks are possibly lost in loss record 1 of 4
==26681== at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
==26681== by 0x4CCC4B8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (new_allocator.h:94)
==26681== by 0x4CCD227: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (basic_string.tcc:631)
==26681== by 0x4CCD30F: std::string::reserve(unsigned long) (basic_string.tcc:512)
==26681== by 0x4CCD5D4: std::string::append(char const*, unsigned long) (basic_string.tcc:310)
==26681== by 0x4C86384: std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) (istream.cc:397)
==26681== by 0x4026ED: main (test.cpp:210)
==26681==
==26681== LEAK SUMMARY:
==26681== definitely lost: 0 bytes in 0 blocks
==26681== indirectly lost: 0 bytes in 0 blocks
==26681== possibly lost: 28 bytes in 1 blocks
==26681== still reachable: 1,728 bytes in 72 blocks
==26681== suppressed: 0 bytes in 0 blocks
==26681== Reachable blocks (those to which a pointer was found) are not shown.
==26681== To see them, rerun with: --leak-check=full --show-reachable=yes
==26681==
==26681== For counts of detected and suppressed errors, rerun with: -v
==26681== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
는 Test.cpp에
bool pending = getline(inputfile, line);
라인 (210) 좀 더 줄 수 있습니다 : 여기
는 Valgrind의에서 보고서입니다 getline이 실패 할 때, 그리고 라인이string
이기 때문에 어떻게 든 메모리를 할당 해제하지 않습니다. 어떻게 이것을 막을 수 있습니까?
readOption도 getline
을 사용합니다. 그러나 string line
이 로컬에 정의되어 있고 메모리를 효과적으로 정리하기 때문에 메모리 누수가 없다고 생각합니까?
편집 1 :이 경우 Valgrind의 불평 왜 나는 확실하지 않다,
bool getnewline(ifstream &inputfile) {
string line;
return getline(inputfile, line);
}
는 이렇게 바보 같다 그러나 :
나는 더미 기능을하여 문제를 "해결"했다 누설 없음. 나는 여전히이 문제에 대해 더 나은/깨끗한 해결책을 찾은 것이다.
표시된 모든 변수에는 자동 저장 기간이 있습니다. 누출 될 수 없습니다. –
valgrind의 불평을 이해하는 방법을 모르겠습니다. – ParoX
오늘 (거의) 같은 질문을 이미 보았습니다 ... –