tags:

views:

94

answers:

2

If you check this link http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=107 it's written:

"For example, the abort() and exit() library functions are never to be used in an object-oriented environment—even during debugging—because they don't invoke objects' destructors before program termination."

Why does the destructor need to be called when one calls exit? (Since the OS guarantees memory will be reclaim whenever a program exits, right?)

+9  A: 

Destructors can, and often do, other operations besides freeing memory and/or resources. They are often used to make certain other guarantees such as user data is written to a file or non-process specific resources are in a known state. The OS won't do these types of operations on exit.

That being said, any program which relies on these types of actions is fundamentally flawed. Use of exit and abort are not the only ways destructors are avoided. There are many other ways a destructor can be circumvented. For example user forcable terminating the process or a power outage.

I definitely disagree with the use of never in the quoted passage. I can think of at least one situation where you absolutely don't want destructors executing: corrupted memory. At the point you detect corrupted memory you can no longer make any guarantees about the code in your process, destructors included. Code which should write data to a file might delete / corrupt it instead.

The only safe thing to do when memory corruption is detected is to exit the process as fast as possible.

JaredPar
Regardless of whether destructors are called, I wouldn't recommend terminating a program via power outage. :)
Justin Ardini
@Justin, Maybe not a power outage but I've certainly taken out my laptop battery with glee as I took my revenge on a hung OS.
JaredPar
It slightly depends on the type of reliance. If we're saying "I'll try to save the user's data", then it's not fundamentally flawed to rely on a destructor to do this - OK, on power outage you won't save, but that might be allowed because there's no way you can save. So you'd want to avoid `abort()` in that program unless saving is impossible anyway, as it would be with corruption. Likewise flushing `stdout` etc. If we're saying, "the user's save file will temporarily be in an unrecoverable state, and the destructor will restore it to a valid state", *then* the program is fundamentally flawed.
Steve Jessop
@Steve, I agree trying isn't a problem as long as you don't depend on it happening
JaredPar
A: 

First I will like to advice that "Don't believe anything blindly you have read." Probably as JaredPar said the destructor may doing some logging things and closing of OS resource handle things. In case you call abort or exit these things will never happen.

But I certainly do not agree with the quote at all. Abort is the best and the fastest way of finding the programming errors in the development cycle. As a developer you certainly don't put abort blindly everywhere in the code, but on the condition you know that should have never happened. You put abort when you know that the other programmer or you have messed up somewhere in the code and it's better to stop than to handle the error. I have gone through a situation where abort has really saved my @$$.

Manoj R