Re: nothrow destructorrs
There are more problems with throwing destructors. In the code:
struct Bad{
~Bad() { throw std::exception(); }
};
int main() {
Bad * ptr = new Bad;
delete ptr;
}
produces a memory leak, as operator delete is never called.
This makes every throwing destructor potentially dangerous as the
class object might be allocated on the free store.
The standard mentions (15.2.3): "If a destructor called during stack
unwinding exits
with an exception, terminate is called (15.5.1). So destructors should
generally catch exceptions and
not let them propagate out of the destructor"
The solution to the problems would be to add additional semantics:
No exception gets out of the destructor body:
Class::~Class() {
doSomething();
}
is semantically equivalent to:
Class::~Class() {
try{
doSomething();
} catch(...) {};
}
unless the destructor has a throw-anything ( throw(...) ) exception
specification
This would spoil the code of anyone who likes throwing from
destructors, of course, but we have a workaround for them: the
explicit throw(...) clause.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]