[英]Valgrind error with std::cin
这是我的代码:
std::string getword()
{
std::string temp;
std::cin >> temp;
return temp;
}
Valgrind在std :: cin >> temp行上引发错误。
这是那些询问的valgrind输出:
HEAP SUMMARY:
==18490== in use at exit: 33 bytes in 1 blocks
==18490== total heap usage: 397 allocs, 396 frees, 12,986 bytes allocated
==18490==
==18490== 33 bytes in 1 blocks are possibly lost in loss record 1 of 1
==18490== at 0x4C2AF8E: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18490== by 0x4EEE3B8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==18490== by 0x4EEF127: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==18490== by 0x4EEF20F: std::string::reserve(unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==18490== by 0x4EA7D14: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==18490== by 0x401681: getword() (netsim.cc:29)
==18490== by 0x401F6E: main (netsim.cc:96)
==18490==
==18490== LEAK SUMMARY:
==18490== definitely lost: 0 bytes in 0 blocks
==18490== indirectly lost: 0 bytes in 0 blocks
==18490== possibly lost: 33 bytes in 1 blocks
==18490== still reachable: 0 bytes in 0 blocks
==18490== suppressed: 0 bytes in 0 blocks
==18490==
==18490== For counts of detected and suppressed errors, rerun with: -v
==18490== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
netsim.cc:96是程序中对getword()的第二次调用。 该代码读取
std::string network = getword();
netsim.cc:29是getword()本身的代码。 第29行是该行
std::cin >> temp
我仍然不明白为什么会这样,但是我设法解决了这个问题。 我有代码
std::string s = getword();
立刻在上面
std::string network = getword();
我同时做了s和网络全局变量,以某种方式解决了问题。
如果有人能解释为什么会这样,我将不胜感激。
我不是valgrind
专家,但我想说这是一个误报。 实际上,它甚至可能是valgrind
自身产生的误报。 在查看“泄漏摘要”并看到泄漏的核心根源时,我感到怀疑:
// The one right below this, that's at the top of that valgrind error
==18490== at 0x4C2AF8E: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18490== by 0x4EEE3B8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
现在,为什么valgrind将报告其共享库中的内存泄漏? 这里似乎没有东西。 valgrind本身在分配内存方面不是很干净(而且valgrind有其自身的泄漏!哦,是的!),或者抛出了一个非常奇怪的误报。 除非GCC的最新分支最近已为std :: string和istream& operator<<
造成了内存泄漏,否则我无法想象这实际上在泄漏。 std::cin
和std::cout
已经使用了很长时间,并且valgrind
会抛出一个错误是没有意义的,尤其是当您的客户端代码没有进行单个new / delete调用时。
简而言之,这里可能会发生一些事情:
valgrind
喝醉了 ! :[ 无论哪种情况,都请忽略它并继续前进。 我怀疑这将以任何关键方式破坏您的netsim
。
希望很快能解决,对不起,我的回答只能这么多,但这是我能提供的最佳方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.