的代碼是相當簡單:Valgrind的示出的std ::矢量<>的alloc的倍比自由多,但沒有內存泄漏
#include <vector>
int main() {
std::vector<int> v;
}
然後我構建和Valgrind的運行:
g++ test.cc && valgrind ./a.out
==8511== Memcheck, a memory error detector
==8511== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8511== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==8511== Command: ./a.out
==8511==
==8511==
==8511== HEAP SUMMARY:
==8511== in use at exit: 72,704 bytes in 1 blocks
==8511== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==8511==
==8511== LEAK SUMMARY:
==8511== definitely lost: 0 bytes in 0 blocks
==8511== indirectly lost: 0 bytes in 0 blocks
==8511== possibly lost: 0 bytes in 0 blocks
==8511== still reachable: 72,704 bytes in 1 blocks
==8511== suppressed: 0 bytes in 0 blocks
==8511== Rerun with --leak-check=full to see details of leaked memory
==8511==
==8511== For counts of detected and suppressed errors, rerun with: -v
==8511== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
問題是雙重的:
(1)「總堆使用率」表示有1個alloc和0空閒。我認爲1分配是因爲std :: vector實例需要堆中的內存塊。沒關係;但爲什麼它在銷燬期間不釋放內存? (2)如果它不釋放它,爲什麼「LEAK SUMMARY」中沒有內存泄漏?
(3)順便說一句,每行之前的==8511==
是什麼意思? (不過我可以在手冊中查到它,你不必回答)
謝謝!
您的問題與C完全無關。 – StoryTeller
看起來像Valgrind 3.11試圖解決這個問題:https://bugzilla.redhat.com/show_bug.cgi?id=1312647 –
有很多東西可能分配C++。它必須爲stdin,stdout和stderr創建文件句柄和緩衝區,並且它可能會創建一個內存池。嘗試刪除'std :: vector <>'看看valgrind說什麼 – gman