Destructors

Hi forum,

If the application ends with the exit(EXIT_FAILURE), does the destructor of the class is called and do the memory cleanup in case the cleanup operations are defined inside the destructor ?

Regards
Sajjadul
According to the C++ Standard (section 3.6.1 Main function)

4 Terminating the program without leaving the current block (e.g., by calling the function std::exit(int) (18.5)) does not destroy any objects with automatic storage duration (12.4). If std::exit is called to end a program during the destruction of an object with static or thread storage duration, the program has
undefined behavior.
closed account (zb0S216C)
sajis997 wrote:
"If the application ends with the exit(EXIT_FAILURE), does the destructor of the class is called and do the memory cleanup in case the cleanup operations are defined inside the destructor ?"

Short answer: for automatic storage, yes.

Though, to expand on this subject a little, this is not always true. Take placement "new" for example: any object created with placement "new" must have its destructor called at the end of the owning pointer's scope; thus, the "new'd" object's destructor must be called before the last remaining pointer to the "new'd" object reaches the end of its residing scope.

In addition, objects allocated with "new" must be "deleted." If you don't call "delete" there will be no destructor call.

Wazzak
Last edited on
@Framework

Short answer: for automatic storage, yes.


It is simply incorrect answer. The C++ Standard

(Automatic objects are not destroyed as a result of calling exit().
closed account (zb0S216C)
@Vlad: Why are you referring to "std::exit( )"? The OP didn't even reference that function. "EXIT_FAILURE" is a integral value defined by the standard to be returned from "main( )" to indicate unsuccessful termination.

Standard wrote:
"Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE , an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined."


Wazzak
Last edited on
@Framework

@Vlad: Why are you referring to "std::exit( )"? The OP didn't even reference that function


From the original post of the thread:

@sajis997

Hi forum,

If the application ends with the exit(EXIT_FAILURE),
closed account (zb0S216C)
Oh. My bad.

Wazzak
closed account (S6k9GNh0)
Agreed. Everything in automatic storage and anything not put into the atexit queue will not be destroyed. Everything in the current block needs to be deallocated and I'd suggest the use of atexit or similar style callback.
Topic archived. No new replies allowed.