What happens to base class destructor if a derived class destructor throws an exception

点点圈 提交于 2019-12-17 18:17:32

问题


It just happened to me I wondered how resources are freed in the following case.

class Base {
  Resource *r;

public:
  Base() { /* ... */ }
  ~Base() {
    delete r; 
  }
};

class Derived : public Base {
public:
  Derived() { /* ... */ }
  ~Derived() {
    /* Suddenly something here throws! */
  }
};

int main() {
  try {
    Derived d;
  } catch(...) {
    /* what happened with Base::r !? */
  }
}

Will the base class destructor be called if the derived class destructor throws? Or will there be a leak?


回答1:


According to §15.2/2:

An object that is partially constructed or partially destroyed will have destructors executed for all of its fully constructed subobjects, that is, for subobjects for which the constructor has completed execution and the destructor has not yet begun execution.

So the base class destructor should be called. That is, just like we know this will clean up the base class:

#include <iostream>

struct foo
{
    ~foo()
    {
        std::cout << "clean" << std::endl;
    }
};

struct bar : foo
{
    bar()
    { // foo is initialized...
        throw 0; // ...so its destructor is run
    }
};

int main()
{
    try
    {
        bar b;
    }
    catch (...)
    {
        std::cerr << "caught" << std::endl;
    }
}

And that this will clean up the member:

#include <iostream>

struct foo
{
    ~foo()
    {
        std::cout << "clean" << std::endl;
    }
};

struct bar
{
    ~bar()
    { // f has been initialized...
        throw 0; // ...so its destructor will run
    }

    foo f;
};

int main()
{
    try
    {
        bar b;
    }
    catch (...)
    {
        std::cerr << "caught" << std::endl;
    }
}

This will also clean up the base class:

#include <iostream>

struct foo
{
    ~foo()
    {
        std::cout << "clean" << std::endl;
    }
};

struct bar : foo
{
    ~bar()
    { // foo has been initialized...
        throw 0; // ...so its destructor will run
    }
};

int main()
{
    try
    {
        bar b;
    }
    catch (...)
    {
        std::cerr << "caught" << std::endl;
    }
}

That's my understanding of the quote.




回答2:


The base class destructor is indeed called. Sample code:

#include 
#include 

class Base {
public:
  Base() { /* ... */ }
  ~Base() {
    printf("Base\n");
  }
};

class Derived : public Base {
public:
  Derived() { /* ... */ }
  ~Derived() {
    printf("Derived\n");
    throw 1;
  }
};

int main() {
  try {
    Derived d;
  } catch(...) {
    printf("CAUGHT!\n");
  }
}

This prints:

Derived
Base
CAUGHT!



回答3:


The base destructor will get called.

In Effective C++, Meyers recommends that exceptions shouldn't leave destructors. Catch the exception inside the destructor and handle it, swallow it or terminate.



来源:https://stackoverflow.com/questions/4508533/what-happens-to-base-class-destructor-if-a-derived-class-destructor-throws-an-ex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!