【问题标题】:Valgrind Error: in use at exit: 72,704 bytes C++ Initialization List weirdness with char*Valgrind 错误:退出时使用:72,704 字节 C++ 初始化列表怪异与 char*
【发布时间】:2015-10-24 20:04:41
【问题描述】:

问题:

我遇到了一个我没想到的奇怪问题。我有一门课叫Answers 在标题中是这样的:

class Answer
{
    char* aText;
    bool b_correct;
public:
    Answer():aText(0){;}  //default constructor
}

主要(测试)驱动代码是这样的:

int main(void) 
{

    static const unsigned int MAX_ANSWERS = 5;
    Answer answers[MAX_ANSWERS];
}

我得到的(出乎意料的)奇怪之处是发生了分配,而且我还没有在代码中的任何地方使用新的。我猜 char* 在初始化列表中调用它。

我正在使用 valgrind 测试我的代码,我得到了 11 个分配和 10 个释放。当我删除 :aText(0) 的初始化器时,额外的分配器消失了。

我知道这是构造错误的代码。我正在按照课程大纲学习如何用 C++ 编写。有人可以帮我了解内存是如何分配的,或者在初始化列表期间发生了什么导致调用 new 吗?

我知道错误来自显示的代码。当我只编译和运行这段代码时,我知道发生了额外的分配。

Valgrind 输出:

==12598== Memcheck, a memory error detector
==12598== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==12598== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==12598== Command: ./Answers
==12598== 
==12598== 
==12598== HEAP SUMMARY:
==12598==     in use at exit: 72,704 bytes in 1 blocks
==12598==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==12598== 
==12598== LEAK SUMMARY:
==12598==    definitely lost: 0 bytes in 0 blocks
==12598==    indirectly lost: 0 bytes in 0 blocks
==12598==      possibly lost: 0 bytes in 0 blocks
==12598==    still reachable: 72,704 bytes in 1 blocks
==12598==         suppressed: 0 bytes in 0 blocks
==12598== Rerun with --leak-check=full to see details of leaked memory
==12598== 
==12598== For counts of detected and suppressed errors, rerun with: -v
==12598== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

平台信息:

Fedora 22

gcc.x86_64 5.1.1-4.fc22

valgrind.x86_64 1:3.10.1-13.fc22

codeblocks.x86_64 13.12-14.fc22

【问题讨论】:

  • 摆脱... 并发布一个完整的课程来展示您正在观察的内容。您可能隐藏了导致分配发生的 ... 后面的代码。
  • 为什么说有分配发生?因为valgrind?此外,编写良好的 C++ 代码不会对new 进行任何显式调用,但会在幕后进行大量自动分配。不要误以为编写 C++ 就是调用newdelete
  • 我不想发布对问题不重要的代码。我在我的代码中设置了中断,这就是我为获得分配而运行的所有内容......是的,valgrind 说它已经发生了。是不是 valgrind 的问题?
  • @user2470057 I didn't want to post code that wasn't important to the problem 然后发布一个显示问题的完整示例。您可能认为不重要的代码可能很重要。或者删除那些省略号,通过给它一个尾随分号来完成该类,将其粘贴在main() 上方,然后从main 中删除省略号。
  • 说真的,剩下的代码不重要。我刚刚用这个当前代码运行了valgrind(......删除了)......我得到了这个:==11857==堆摘要:==11857==在退出时使用:1个块中的72,704字节==11857==总堆使用量:1 次分配,0 次释放,72,704 字节分配 ==11857== 仍然可以访问:72,704 字节在 1 个块中

标签: c++ gcc valgrind initializer-list initialization-list


【解决方案1】:

这是一个已知的 GCC 5.1 错误,而不是 valgrind 错误。

这里的详细信息: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64535

可能的解决方法: 将 GCC 降级到早期版本或等待 Valgrind 更新此错误的修复程序。这两种解决方案都由各自的社区开发。

【讨论】:

  • 有什么进展吗?
  • Bug 仍然存在于 5.4 中
  • 我一定是误会了,因为当我使用 Clang 3.8 编译时,我得到了完全相同的 Valgrind 错误
  • 我在 Travis CI 上也遇到了 Clang 和 GCC 完全相同的错误。这是leak with GCCleak with Clang 的示例。 (valgrind 错误出现在最后。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多