[英]Getting a "still reachable" in LEAK SUMMARY in Valgrind when using std::ios::sync_with_stdio(fasle) to print out
Consider the simple code below:考虑下面的简单代码:
#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;
}
When using the above printing function I get a "still reachable" in the LEAK SUMMARY when profiling with valgrind.使用上述打印功能时,在使用 valgrind 进行分析时,我会在 LEAK SUMMARY 中看到“仍然可以访问”。
I used the following for compiling: (gcc version 9)我使用以下进行编译:(gcc 版本 9)
g++ --std=c++17 -Wall sorting_stl.cc -o sorting_stl.o
and the following valgrind command和以下 valgrind 命令
valgrind --leak-check=full --show-leak-kinds=all -v ./sorting_stl.o
The full LEAK SUMMARY at the end:完整的泄漏总结在最后:
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
Removing the std::ios::sync_with_stdio(false);
删除std::ios::sync_with_stdio(false);
and using std::endl;
并使用std::endl;
resolves the error.解决错误。 I don't understand the reason though why this happens, or if it is an error here.我不明白为什么会发生这种情况,或者这里是否有错误。 Is it a good idea in general to use std::sync_with_stdio(false);
通常使用std::sync_with_stdio(false);
This counts as a "false positive";这算作“误报”; some aspects of the standard library deliberately don't do clean-up.标准库的某些方面故意不进行清理。 This is one of them. 这是其中之一。
It's not a leak that'll get worse over time.这不是随着时间的推移会变得更糟的泄漏。 Don't worry about it.别担心。
Ideally Valgrind would be neighbourly and would filter this out, but apparently it's been an issue for a long time .理想情况下,Valgrind 应该是友好的,并且会过滤掉它,但显然它已经存在很长时间了。
You can read more about the various report levels in the Valgrind FAQ .您可以在 Valgrind 常见问题解答中阅读有关各种报告级别的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.