RAII Failure - Why Does this C++ Code Leak? - throw in ctor in try block prevents dtor
问题 The output of the program below is: begin try Object() ctor begin catch Why is the Holder class's destructor not called? Is this a memory leak? Is it possible to call the Holder class's destructor without rethrowing? #include <iostream> #include <exception> class Object { public: Object() { std::cout << "Object() ctor" << std::endl; } ~Object() { std::cout << "~Object() dtor" << std::endl; } }; class Holder { public: Holder() :myObjectP( new Object() ) { throw std::exception(); } ~Holder() {