1

这是我的代码:

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

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

4

1 回答 1

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在它上面抛出错误是没有意义的,特别是当您的客户端代码没有进行单个 new/delete 调用时。

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

  1. valgrind 正在泄漏。(也许?它源自 valgrind 函数)
  2. GCC 正在泄漏?(不敢说这个)
  3. valgrind!:[

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

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

于 2013-04-15T05:19:40.977 回答