Valgrind错误:退出时正在使用:72,704字节C ++带有char *的初始化列表怪异 [英] Valgrind Error: in use at exit: 72,704 bytes C++ Initialization List weirdness with char*

查看:175
本文介绍了Valgrind错误:退出时正在使用:72,704字节C ++带有char *的初始化列表怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我遇到了一个我没想到的怪异问题。我有一个叫做Answers
的类,并且标题内是这样的:

I have a weird issue that I wasn't expecting. I have a class called Answers and within the header is this:

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

主要(测试)驱动程序代码如下:

The main (testing) driver code is this:

int main(void) 
{

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

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

The (unexpected) weirdness I am getting is that there is an alloc happening, and I haven't used a new anywhere in my code yet. I'm guessing that the char* is calling this in the initialization list.

我正在使用valgrind测试我的代码,并且我得到11个分配和10个释放。当我删除:aText(0)的初始化程序时,多余的分配就消失了。

I am using valgrind to test my code, and I'm getting 11 allocs and 10 frees. When I remove the initializer of :aText(0), the extra alloc goes away.

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

I get that this is badly constructed code. I am following a course outline to learn how to write in C++. Can someone please help me understand how the memory is allocated or what's happening during the initialization list to cause a call to new?

我知道错误来自显示的代码。我知道在编译并运行此代码时会发生额外的分配。

I know the error is coming from the code shown. I know the extra alloc is happening When I compile and run just this code.

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

Fedora 22

gcc.x86_64 5.1.1-4.fc22

gcc.x86_64 5.1.1-4.fc22

val grind.x86_64 1:3.10.1-13.fc22

valgrind.x86_64 1:3.10.1-13.fc22

codeblocks.x86_64 13.12-14.fc22

codeblocks.x86_64 13.12-14.fc22

推荐答案

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

This is a known GCC 5.1 bug, not a valgrind bug.

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

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

Possible workarounds: Downgrade GCC to an earlier version or wait for Valgrind to update a fix for this error. Both solutions are being worked on by their respective communities.

这篇关于Valgrind错误:退出时正在使用:72,704字节C ++带有char *的初始化列表怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆