red floyd wrote:
....
>
Because if T has a trivial destructor, then no harm is done by invoking
its destructor.
I don't think "no harm" is entirely clear. I'm not suggesting this is
good code, but there have been badly designed systems where I have
needed to not call the destructor on system exit because bad things
would in fact happen. Hence, as a generalization, I don't think "no
harm" is supportable.
... If T has a non-trivial destructor, then failure to
invoke it can cause problems. Therefore, the destructor should be invoked.
Not all non-trivial destructors (for globals) need to be called. The
operating system does a much more efficient cleanup on exit than a
e.g. this code (brain dump - not checked)
#include <vector>
std::vector<int> * foo = new std::vector<int>(10000000);
int main()
{
}
will not delete the object pointed to by foo, however on all popular
operating systems (I don't talk about embedded ones), this code is well
behaved.
In general, you can't really bargain on global destructors being called
anyway because there are ways of termination that do not call the final
cleanup routines (memory fault, power failure, failure to catch
exception, memory depletion, unresovable page faults etc just to mention
some). So if your code truly depends on global destructors to be
called, you're in for trouble.
For example, if your code uses temporary files, you may want to "lock"
the file and on startup, cleanup all unlocked temporary files. You
could do the same for all persistent OS objects.
So the point is, your generalization is clearly incorrect. While it is
"preferable" to have destructors of global objects called, it is
certainly not required all the time and it certainly cannot be depended
on all the time.
All generalizations are wrong.