在瓦尔格林德的泄漏摘要中使用 std::ios::sync_with_stdio(fasle) 打印时获取"still reachable"



考虑下面的简单代码:

#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 分析时,我在泄漏摘要中得到"仍然可以访问"。

我使用了以下内容进行编译:(gcc 版本 9(

g++ --std=c++17 -Wall sorting_stl.cc -o sorting_stl.o

和以下瓦尔格林德命令

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);是个好主意吗

没关系。

这算作"误报";标准库的某些方面故意不进行清理。这是其中之一。

这不是一个会随着时间的推移而变得更糟的泄漏。别担心。

理想情况下,瓦尔格林德会是邻居,并会过滤掉这一点,但显然这已经是一个问题很长时间了。

您可以在瓦尔格林德常见问题解答中阅读有关各种报告级别的更多信息。

最新更新