Quote
Unhandled exception at 0x004fcfd9 in fhx.exe: 0xC0000005: Access violation reading location 0x0000006f.
The crash doesn't seem to occur when the derived class function is made non-virtual. In fact, all virtual functions of the derived classes will crash, but the non-virtual functions are perfectly fine, as are all member variables. No traces of memory corruption.
// base class of AAA
class XXX
{
private:
int baz;
protected:
XXX() { baz = 666; }
public:
void SetBaz(double v) { baz = v; }
};
// base class for BBB and CCC
class AAA: public XXX
{
private:
int foo;
public:
AAA() { int foo = 0; }
void SetFoo(int v) { foo = v; }
virtual void Hello() { printf(" (AAA::Hello) "); }
};
// derived class
class BBB: public AAA
{
public:
BBB() {}
void Hello() { printf(" (BBB::Hello) "); }
};
// derived class, without virtual function
class CCC: public AAA
{
public:
CCC() {}
};
void Test()
{
// set to false and all is good
bool weWantToCrash = true;
// create some instances of both derived classes
BBB *b = new BBB;
CCC *c = new CCC;
// this causes no trouble
b->SetBaz( 111 );
c->SetBaz( 222 );
// but this...
if (weWantToCrash) {
void *bptr = b;
void *cptr = c;
((XXX*)bptr)->SetBaz( 111 ); // this seems to screw up the vtable of b
((XXX*)cptr)->SetBaz( 222 ); // this seems to screw up the vtable of c
print("...but we're still running...\n");
}
// non virtual functions will work fine
b->SetFoo(1);
c->SetFoo(2);
print("SetFoo no problem...\n");
// now this will crash if weWantToCrash == true
b->Hello();
c->Hello();
// just for kicks, perhaps casting to AAA will work?
AAA *ptr = NULL;
Console.Print("going to say hello to b...");
ptr = b;
ptr->Hello(); // nope, this will crash as well if weWantToCrash == true
print("OK...\n");
Console.Print("going to say hello to c...");
ptr = c;
ptr->Hello(); // nope, this will crash as well if weWantToCrash == true
print("OK...\n");
// looks like virtual functions are now screwed up!
}
This happens with MSVC2005E.












