Mlha Mlha - 2 months ago 29
C++ Question

Is calling non-member function from destructor OK?

In C++, is it OK to call a non-member function (either a free function or a member of other objects) from within the destructor? I am calling a (non-virtual, although in this case it shouldn't really matter) method of an object of a different class. This method crashes on trying to access its members.

On the other hand, if this different object is a child of the destructed object (Qt), does this matter?


In general a destructor can call any function it needs to properly destroy the object. However, there are a couple of caveats:

  1. If the function called from a destructor throws an exception, the exception must be caught and handled in the destructor.

  2. The function called from a destructor must not unconditionally create and destroy objects of the type to which the destructor belongs (since this will result in infinite recursion).