这是我的代码:
std::string getword()
{
std::string temp;
std::cin >> temp;
return temp;
}
Valgrind在行std :: cin>>上抛出错误温度。
以下是那些提出问题的人的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和网络全局变量,并以某种方式解决了问题。
如果有人能解释为什么会这样,我会很感激。
答案 0 :(得分:0)
我不是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
会错误地使用valgrind
毫无意义,尤其是当您的客户端代码没有创建一个新的/删除时调用
因此,简而言之,这里可能会发生一些事情:
netsim
醉酒 ! :在任何一种情况下,忽略它并继续前进。我怀疑这会以任何关键的方式破坏你的{{1}}。
我希望它很快就会消失,我很抱歉我的回答只能这么说,但这些是我能给出的最佳镜头。