When you override a member function that is not virtual in a class with no virtual functions, VS compilers occurs the “_BLOCK_TYPE_IS_VALID” error.
For example,
class A{
public:
int a;
public:
void func(){}
~A(){}
};
class B : public A{
public:
virtual void func(){}
~B(){}
};
int main(void){
A* a = new B();
delete a; // error!
return 0;
}
I guess this is because in main(), the a has vtable but the compiler misses it and can’t get the exact size of the header?
Somebody can get my curiosity about this shattered?
Thanks in advance.
You can remove
A::func()and the program is still erroneous.The real reason is that
A::~A()(notB::~B()) is being called on an object of typeB.See C++ FAQ § 20.7 “When should my destructor be
virtual?”