Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Virtual destructor Problem (http://www.go4expert.com/forums/virtual-destructor-problem-t10216/)

humhaingaurav 24Apr2008 12:02

Virtual destructor Problem
Hi Guys,
Can someone please explain me why this is happening

using namespace std;

class a {

int a1; // If I remove this it'll work fine
a() {
cout << "Constructor of a \n";
~a() {
cout << "Destructor of a \n";


class b : public a{

b() {
cout << "constructor of b \n";
virtual ~b() { // If I remove virtual from here ... it'll work fine
cout << "destructor of b\n";


int main() {
a *p = new b;

delete p;

return 0;

Result is :
Constructor of a
constructor of b
Destructor of a
a.out(1563) malloc: *** error for object 0x100154: Non-aligned pointer
being freed
*** set a breakpoint in malloc_error_break to debug

I know that if I'll put virtual in front of the destructor of the base class code will work fine. But, I want to use it the above way. Can someone please explain me the reason behind this kind of behavior?

Thanks in advance

All times are GMT +5.5. The time now is 13:34.