繁体   English   中英

使用 std::ios::sync_with_stdio(fasle) 打印时,在 Valgrind 的 LEAK SUMMARY 中获取“仍然可达”

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM