Access violation when calling some MFC default constructors
I am attempting to ask this question again due to m开发者_JAVA技巧y failure to state the question clearly yesterday. Basically, I have an access violation error described in the comment in the code below... any idea why?
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;
}
- Check the call stack properly.
- Ensure object is allocated properly, try allocating it on stack (and not just by shortcut).
- Check the other classes do not have
#pragma
packing conflicts. - Try removing some data-members from class
C
.
I've found the solution to my problem. The cause of the problem is that Class A is built to a dll with a different struct alignment than class B and C.
精彩评论