Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling virtual function from destructor

Tags:

c++

virtual

Is this safe ?

class Derived:  public PublicBase, private PrivateBase {  ...      ~Derived()    {       FunctionCall();    }     virtual void FunctionCall()    {       PrivateBase::FunctionCall();    } }  class PublicBase {    virtual ~PublicBase(){};    virtual void FunctionCall() = 0; }  class PrivateBase {    virtual ~PrivateBase(){};    virtual void FunctionCall()    {     ....    } }   PublicBase* ptrBase = new Derived(); delete ptrBase; 

This code crases sometimes with IP in a bad address.

That is not a good idea to call a virtual function on constructor is clear for everyone.

From articles like http://www.artima.com/cppsource/nevercall.html I understand that destructor is also a not so good place to call a virtual function.

My question is "Is this true ?" I have tested with VS2010 and VS2005 and PrivateBase::FunctionCall is called. Is undefined behavior ?

like image 703
cprogrammer Avatar asked Aug 23 '12 13:08

cprogrammer


People also ask

Can I call virtual function from destructor?

Calling virtual functions from a constructor or destructor is considered dangerous most of the times and must be avoided whenever possible. All the C++ implementations need to call the version of the function defined at the level of the hierarchy in the current constructor and not further.

Can we call virtual function in destructor in C++?

No, the "full" object isn't involved in constructor and destructor semantics. Virtual function calls from constructors and destructors go to the version of the function for the type whose constructor or destructor is being run, not necessarily the most derived type.

Why should calls to virtual functions be avoided in constructors and destructors?

So, Don't invoke virtual functions from constructors or destructors that attempts to call into the object under construction or destruction, Because the order of construction starts from base to derived and the order of destructors starts from derived to base class.

Do not invoke virtual functions from constructors or destructors?

As a general rule, you should never call virtual functions in constructors or destructors. If you do, those calls will never go to a more derived class than the currently executing constructor or destructor. In other words, during construction and destruction, virtual functions aren't virtual.


2 Answers

I am going to go against the flow here... but first, I must assume that your PublicBase destructor is virtual, as otherwise the Derived destructor will never be called.

It is usually not a good idea to call a virtual function from a constructor/destructor

The reason for this is that dynamic dispatch is strange during these two operations. The actual type of the object changes during construction and it changes again during destruction. When a destructor is being executed, the object is of exactly that type, and never a type derived from it. Dynamic dispatch is in effect at all time, but the final overrider of the virtual function will change depending where in the hierarchy you are.

That is, you should never expect a call to a virtual function in a constructor/destructor to be executed in any type that derived from the type of the constructor/destructor being executed.

But

In your particular case, the final overrider (at least for this part of the hierarchy) is above your level. Moreover, you are not using dynamic dispatch at all. The call PrivateBase::FunctionCall(); is statically resolved, and effectively equivalent to a call to any non-virtual function. The fact that the function is virtual or not does not affect this call.

So yes it is fine doing as you are doing, although you will be forced to explain this in code reviews as most people learn the mantra of the rule rather than the reason for it.

like image 134
David Rodríguez - dribeas Avatar answered Oct 09 '22 21:10

David Rodríguez - dribeas


Is this safe ?

Yes. Calling a virtual function from a constructor or destructor dispatches the function as if the object's dynamic type were that currently being constructed or destroyed. In this case, it's called from the destructor of Derived, so it's dispatched to Derived::FunctionCall (which, in your case, calls PrivateBase::FunctionCall non-virtually). All of this is well defined.

It's "not a good idea" to call virtual functions from a constructor or destructor for three reasons:

  • It will cause unexpected behaviour if you call it from a base class and (erroneously) expect it to be dispatched to an override in a derived class;
  • It will cause undefined behaviour if it is pure virtual;
  • You'll keep having to explain your decision to people who believe that it's always wrong to that.
like image 29
Mike Seymour Avatar answered Oct 09 '22 23:10

Mike Seymour