调用某些MFC默认构造函数时发生访问冲突

Access violation when calling some MFC default constructors

本文关键字:访问冲突 构造函数 默认 MFC 调用      更新时间:2023-10-16

由于昨天没有说清楚,我想再问一遍这个问题。基本上,我有一个访问冲突错误描述在下面的代码注释中…知道为什么吗?

Class A 
{
private:
    BOOL a;
    BOOL b;
    int i;
public:
    A() {a = FALSE; b = FALSE; i = 0;}
....
}

Class B : public A 
{
public:
    B() {} // empty constructor
....
}
Class C
{
public:
    C() {} // <-- when the constructor is calling the CButton and CCombobox 
           // default constructor for the member "cb" and "button", it overrides 
           // the address space of some of the variables defined in class A 
           // (e.g. a, and b would be changed to some garbage)
           // Basically, any variable defined below 'y' will have similar 
           // problems, though not exactly the same variables from 'y' will 
           // be changed..
private:
    int x;
    B y;
    CCombobox cb;
    CButton button;
}
  • 正确检查调用栈
  • 确保对象被正确分配,尝试在堆栈上分配(而不仅仅是通过快捷方式)。
  • 检查其他类是否有#pragma包装冲突
  • 尝试从C类中删除一些数据成员。

我已经找到解决问题的方法了。问题的原因是类A被构建为具有与类B和c不同的结构对齐方式的dll。