【问题标题】:Valgrind error with std::cinstd::cin 的 Valgrind 错误
【发布时间】:2013-04-15 04:52:54
【问题描述】:

这是我的代码:

 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 和 network 全局变量,不知何故问题得到了解决。

如果有人能解释为什么会这样,我将不胜感激。

【问题讨论】:

  • 什么是错误 - 你不认为你应该至少在问题中添加该信息吗?
  • 我同意@user93353。 “Valgrind 抛出错误”并不是特别有用。介意编辑您的帖子以供我们使用吗?

标签: c++ input valgrind cin


【解决方案1】:

我不是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&amp; operator&lt;&lt; 产生了内存泄漏,否则我无法想象这实际上会泄漏。 std::cinstd::cout 已经使用了很长时间,valgrind 会在上面抛出错误是没有意义的,尤其是当您的客户端代码没有进行单个 new/delete 调用时。

所以,简而言之,这里可能会发生一些事情:

  1. valgrind 正在泄漏。 (也许?它源自 valgrind 函数)
  2. GCC 泄漏? (犹豫不决)
  3. valgrind喝醉了! :[

在任何一种情况下,忽略它并继续前进。我怀疑这会以任何关键的方式破坏您的netsim

我希望它很快就会好起来,很抱歉我的回答只能说这么多,但这些是我能给出的最好的镜头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2014-01-19
    • 2011-10-12
    • 2018-06-19
    • 2011-06-12
    • 2018-03-05
    相关资源
    最近更新 更多