[英]Getting a "still reachable" in LEAK SUMMARY in Valgrind when using std::ios::sync_with_stdio(fasle) to print out
考慮下面的簡單代碼:
#include <iostream>
#include <algorithm>
#include <vector>
void print_vector( const std::vector<int> &inputVector )
{
std::ios_base::sync_with_stdio(false);
for ( const int &p : inputVector )
{
std::cout << p << " ";
}
std::cout << "\n";
}
int main() {
std::vector<int> s{ 5, 7, 4, 2, 8, 6, 1, 9, 0, 3 };
std::sort( s.begin(), s.end(), [](const int &a, const int &b)
{
return a > b;
});
print_vector( s );
return 0;
}
使用上述打印功能時,在使用 valgrind 進行分析時,我會在 LEAK SUMMARY 中看到“仍然可以訪問”。
我使用以下進行編譯:(gcc 版本 9)
g++ --std=c++17 -Wall sorting_stl.cc -o sorting_stl.o
和以下 valgrind 命令
valgrind --leak-check=full --show-leak-kinds=all -v ./sorting_stl.o
完整的泄漏總結在最后:
LEAK SUMMARY:
definitely lost: 0 bytes in 0 blocks
indirectly lost: 0 bytes in 0 blocks
possibly lost: 0 bytes in 0 blocks
still reachable: 122,880 bytes in 6 blocks
suppressed: 0 bytes in 0 blocks
刪除std::ios::sync_with_stdio(false);
並使用std::endl;
解決錯誤。 我不明白為什么會發生這種情況,或者這里是否有錯誤。 通常使用std::sync_with_stdio(false);
這算作“誤報”; 標准庫的某些方面故意不進行清理。 這是其中之一。
這不是隨着時間的推移會變得更糟的泄漏。 別擔心。
理想情況下,Valgrind 應該是友好的,並且會過濾掉它,但顯然它已經存在很長時間了。
您可以在 Valgrind 常見問題解答中閱讀有關各種報告級別的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.