Runtime Error for virtual derived class destructor

P

pritesh kadam

I declared derived class destructor as virtual.

CODE:

#include<iostream>
using namespace std;

class Base{ //Base class
int x; public:
Base()
{cout<<"Base Constructed\n";}
~Fred() //Base destructor not virtual
{cout<<"Base Destructed\n";}
};

class Der: public Base //Derived
{
public:
Der(){cout<<"Derived Constructor"; }

virtual ~Der() //Derived class destructor virtual
{ cout<<"Derived Destructed";}
};

int main()
{
Base *f=new Der();
delete f;

}


It gets compiled correctly. But on execution, following is o/p :

O/p

Base Constructed
Derived Constructor
Base Destructed
*** glibc detected *** ./trial: free(): invalid pointer: 0x0957b00c ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x19b591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x19cde8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x19fecd]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xd9e741]
../trial[0x80487fe]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x146bd6]
../trial[0x8048701]
======= Memory map: ========
00113000-0012e000 r-xp 00000000 08:06 390404 /lib/ld-2.11.1.so
0012e000-0012f000 r--p 0001a000 08:06 390404 /lib/ld-2.11.1.so
0012f000-00130000 rw-p 0001b000 08:06 390404 /lib/ld-2.11.1.so
00130000-00283000 r-xp 00000000 08:06 394800 /lib/tls/i686/cmov/libc-2.11.1.so
00283000-00284000 ---p 00153000 08:06 394800 /lib/tls/i686/cmov/libc-2.11.1.so
00284000-00286000 r--p 00153000 08:06 394800 /lib/tls/i686/cmov/libc-2.11.1.so
00286000-00287000 rw-p 00155000 08:06 394800 /lib/tls/i686/cmov/libc-2.11.1.so
00287000-0028a000 rw-p 00000000 00:00 0
00538000-0055c000 r-xp 00000000 08:06 394808 /lib/tls/i686/cmov/libm-2.11.1.so
0055c000-0055d000 r--p 00023000 08:06 394808 /lib/tls/i686/cmov/libm-2.11.1.so
0055d000-0055e000 rw-p 00024000 08:06 394808 /lib/tls/i686/cmov/libm-2.11.1.so
0059e000-005bb000 r-xp 00000000 08:06 390462 /lib/libgcc_s.so.1
005bb000-005bc000 r--p 0001c000 08:06 390462 /lib/libgcc_s.so.1
005bc000-005bd000 rw-p 0001d000 08:06 390462 /lib/libgcc_s.so.1
008f2000-008f3000 r-xp 00000000 00:00 0 [vdso]
00ce3000-00dcc000 r-xp 00000000 08:06 262327 /usr/lib/libstdc++.so.6.0.13
00dcc000-00dcd000 ---p 000e9000 08:06 262327 /usr/lib/libstdc++.so.6.0.13
00dcd000-00dd1000 r--p 000e9000 08:06 262327 /usr/lib/libstdc++.so.6.0.13
00dd1000-00dd2000 rw-p 000ed000 08:06 262327 /usr/lib/libstdc++.so.6.0.13
00dd2000-00dd9000 rw-p 00000000 00:00 0
08048000-08049000 r-xp 00000000 08:06 137559 /home/pritesh/C/trial
08049000-0804a000 r--p 00000000 08:06 137559 /home/pritesh/C/trial
0804a000-0804b000 rw-p 00001000 08:06 137559 /home/pritesh/C/trial
0957b000-0959c000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b77f9000-b77fb000 rw-p 00000000 00:00 0
b7809000-b780c000 rw-p 00000000 00:00 0
bf899000-bf8ae000 rw-p 00000000 00:00 0 [stack]
Aborted
pritesh@pritesh-desktop:~/C$
 
V

Victor Bazarov

I declared derived class destructor as virtual.

CODE:

#include<iostream>
using namespace std;

class Base{ //Base class
int x; public:
Base()
{cout<<"Base Constructed\n";}
~Fred() //Base destructor not virtual

I think you meant

~Base()
{cout<<"Base Destructed\n";}
};

class Der: public Base //Derived
{
public:
Der(){cout<<"Derived Constructor"; }

virtual ~Der() //Derived class destructor virtual
{ cout<<"Derived Destructed";}
};

int main()
{
Base *f=new Der();
delete f;

Since 'Base' class destructor is not virtual, deleting a derived object
via the base class pointer has undefined behavior. A "run-time error"
is totally acceptable result since the language does not impose any
particular behavior in this case. Just like absence of any "error"
would be, as well. Or your hard drive formatted. Or nasal demons.

Read up on "undefined behavior".
}


It gets compiled correctly. But on execution, following is o/p :

O/p

Base Constructed
Derived Constructor
Base Destructed
*** glibc detected *** ./trial: free(): invalid pointer: 0x0957b00c ***
[...]

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top