我得到一個內存泄漏getline,我不知道爲什麼或如何阻止它。getline()valgrind內存泄漏
下面是從Valgrind的報告:
==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一些更行:
string line;
bool pending = getline(inputfile, line);
int round = readOption(inputfile);
int num = readOption(inputfile);
我認爲它有話當getline失敗,並且因爲行是string
,它不知何故不會釋放該內存。我如何防止這種情況?
readOption也使用getline
但我認爲它沒有內存泄漏,因爲string line
在本地定義,然後超出範圍,有效地清理內存?
編輯1:
我已讓虛擬函數「解決」問題:
bool getnewline(ifstream &inputfile) {
string line;
return getline(inputfile, line);
}
但它似乎愚蠢的做到這一點,我不知道爲什麼Valgrind是抱怨,如果有沒有泄漏。我仍然對這個問題採取更好的/乾淨的解決方案。
您顯示的所有變量都具有自動存儲持續時間。他們不能泄漏。 –
我不知道如何理解valgrind的抱怨,然後 – ParoX
我今天已經看到這個(幾乎)相同的問題... –