成员初始化:GCC 中的错误还是我的想法?

发布于 2024-09-28 18:24:57 字数 980 浏览 5 评论 0原文

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

我将简化代码,如果它是一个 GCC bug,我确信它是我正在组合/继承/等的特定类的组合,但我保证这抓住了问题的本质。在初始化之前没有任何内容使用此成员变量,并且在完全构造之前没有任何内容使用新创建的对象。这个成员的初始化确实是我在主体中做的第一件事,当内存损坏发生时,valgrind 说它在我初始化变量的行上。 Valgrind 说这是大小为 4 的无效写入。

相关头代码:


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

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


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

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


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

编辑:请参阅我的“答案”是什么原因造成的。读完后,有人可以向我解释为什么它会产生影响吗?我没有更改标头中的任何内容,显然目标文件正在重建,因为当我将它们放入时出现了我的打印语句,并且在一个构建下看不到错误,但在另一个构建下看不到错误?

为了得到更好的解释,我将其标记为这个问题的答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2024-10-05 18:24:57

对于后代:

似乎 make 脚本由于某种原因没有拾取对这些文件的更改。手动删除对象而不是让我们的“干净”目标出现在 makefile 中会导致完全重建(这需要一些时间),并且问题消失了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文