成员初始化:GCC中的错误或我的想法? [英] Initialization of member: bug in GCC or my thinking?

查看:126
本文介绍了成员初始化:GCC中的错误或我的想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举类型定义在我的类的私有部分。我有一个这种类型的成员定义。当我尝试在构造函数体中初始化此成员时,我在运行时遇到内存损坏问题。当我通过同一个构造函数中的初始化列表初始化它时,我不会得到内存损坏的问题。我做错了什么?



我会简化代码,如果它是一个GCC错误,我确信它是一个组合的特定类我组合/继承/等等,但我承诺这捕获的问题的本质。没有什么在初始化之前使用这个成员变量,没有什么使用新创建的对象,直到它被完全构造。这个成员的初始化确实是我在身体中做的第一件事,当内存损坏发生时,valgrind说它是在我初始化变量的行。



相关标题代码:

 

private:
枚举StateOption {original = 0,blindside};
StateOption currentState;

相关的.cpp代码(导致内存损坏和崩溃):

 

MyClass :: MyClass(AClass * classPtr):
BaseClass(s​​td :: string ),classPtr)
{
currentState =
...
}

相关.cpp代码不会导致内存损坏和崩溃):

 

MyClass :: MyClass(AClass * classPtr) :
BaseClass(s​​td :: string(some_setting),classPtr),
currentState(original)
{
...
}


编辑:见我的答案阅读后,任何人可以向我解释为什么它有所作为?我没有更改标题中的任何东西,显然目标文件正在重建,因为我的打印语句出现时,我把它们,缺乏看到一个构建,但不是其他的错误。

$ b

解决方案

对于一个很好的解释,我会将其标记为这个问题的答案。 >对于后代:



似乎make脚本不会因为某些原因而改变这些文件。手动删除对象而不是让makefile中的clean目标导致完全重建(需要一些时间),并且问题消失了。


I've got an enum type defined in the private section of my class. I have a member of this type defined as well. When I try to initialize this member in the constructor body, I get memory corruption problems at run-time. When I initialize it through an initialization list in the same constructor instead, I do not get memory corruption problems. Am I doing something wrong?

I'll simplify the code, and if it is a GCC bug I'm sure that it's a combination of the specific classes I'm combining/inheriting/etc., but I promise that this captures the essence of the problem. Nothing uses this member variable before it is initialized, and nothing uses the newly created object until after it is fully constructed. The initialization of this member is indeed the first thing I do in the body, and when the memory corruption happens, valgrind says it is on the line where I initialize the variable. Valgrind says that it is an invalid write of size 4.

Pertinent header code:


private:  
  enum StateOption{original = 0, blindside};    
  StateOption currentState;

pertinent .cpp code (causes memory corruption and crash):


MyClass::MyClass(AClass* classPtr) : 
  BaseClass(std::string("some_setting"),classPtr)
{
  currentState = original;
  ...
}

pertinent .cpp code (does not cause memory corruption and crash):


MyClass::MyClass(AClass* classPtr) : 
  BaseClass(std::string("some_setting"),classPtr),
  currentState(original)
{
  ...
}
  

edit: see my "answer" for what was causing this. After reading it, can anybody explain to me why it made a difference? I didn't change anything in the header, and obviously the object file was being rebuilt because of my print statements appearing when I put them in and the lack of seeing the bug under one build but not the other?

For a good explanation, I'll mark it as the answer to this question.

解决方案

For posterity:

It appears as though the make script isn't pickup up the changes to these files for some reason. Manually deleting the objects rather than letting our "clean" target in the makefile caused a full rebuild (which took some time), and the problem disappeared.

这篇关于成员初始化:GCC中的错误或我的想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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