Virtual Destructors?

A

amit_edge

Can anyone tell me the reason for this behaviour of virtual
destructors?

#include<stdio.h>
class a
{
public:
int i;
a() { i=10; }
virtual ~a() { printf("\nDestroying A"); }
};
class b: public a
{
public:
~b(){ printf("\nDestroying B"); }
};
void main()
{
b *b1=new b;
a *a1=b1;
a1->~a(); //calls Destructor B and A
printf("%d\n",a1->i); // prints 10
delete b1; //calls Destructor A only!!!!
printf("%d\n",a1->i); // prints junk
}
 
R

roberth+news

(e-mail address removed) wrote:
| Can anyone tell me the reason for this behaviour of virtual
| destructors?

Your program is ill-formed, and when run, (after changing void main
to int main, so that it will compile, of course) anything can
happen.
 
S

Sumit Rajan

Can anyone tell me the reason for this behaviour of virtual
destructors?

#include<stdio.h>
class a
{
public:
int i;
a() { i=10; }
virtual ~a() { printf("\nDestroying A"); }
};
class b: public a
{
public:
~b(){ printf("\nDestroying B"); }
};

You want to change this to "int main()". For more details, see:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
void main()
{
b *b1=new b;
a *a1=b1;
a1 and b1 point to the same object.
a1->~a(); //calls Destructor B and A
now you destroy the object.

printf("%d\n",a1->i); // prints 10
Here, you invoke undefined behaviour (UB) by trying to play around with an
object that does not exist.
delete b1; //calls Destructor A only!!!!
You delete an object twice! Further UB, but now it really doesn't matter
anymore since we're already in the realm of undefined behaviour.
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.2
printf("%d\n",a1->i); // prints junk

More of the same.
UB+anything = UB

Regards,
Sumit.
 
A

amit_edge

That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??
 
J

Jean-Sebastien Samson

That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??

What Sumit told you is that you delete an object which no longer exists
(even if the memory was not released). This is evil and results in undefined
behaviour. Your program might also legitimately erase your hard drive and
launch a nuclear missile. The standard authorizes this. Therefore, it is
impossible to explain to you why you specifically get this result as any
result would qualify as undefined behaviour (even working perfectly during
all the development phase and blowing latter in the face of your most
important customer).
 
P

Peter Koch Larsen

That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??
delete has a dual purpose:
1) destroy the object by calling the objects destructor
2) releasing the memory.
You are only allowed to call a destructor once - calling it more times than
one causes undefined behaviour. Also, there is almost never any reason to
call the destructor explicitly as you did. What was the purpose of that?

/Peter
 
S

Sumit Rajan

That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not

From the C++ Standard, 12.4/13:
"Once a destructor is invoked for an object, the object no longer exists;
....."

Maybe this will be helpful, too:
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9
destroyed. Why does "delete b1" then call the destructor of A only??

As mentioned in Jean-Sebastien Samson's posting, you are now attempting to
destroy an object that does not exist.

Regards,
Sumit.
 
R

Rolf Magnus

That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed.

The object is destroyed. That's what the destructor does. But the memory
isn't released.
Why does "delete b1" then call the destructor of A only??

a1 and b1 are just pointers. By writing "a *a1=b1;", you just made a1 point
to the very same object that b1 points to. So there are _not_ two object,
but still only one. By "a1->~a()", you destroy that object, so now there is
no object left. You still have the memory that the object was in, but C++
doesn't allow you to do much with it. Especially not to try to destroy the
(now non-existing) object again. "delete b1;", when used correctly, does
that. It first destroys the object, then releases the memory it occupied.
Destructor B destroyed successfully in the first attempt??

At that point, there still was an object that could be destroyed.
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top