Force unwinding stack, exit gracefully.

J

Jim King

A is an RAII class.

int main()
{
A a;
(void)a;

return 0;
}

Definitely, object "a" will be destructed.

Now, let's use exit().

int main()
{
A a;
(void)a;

exit(0);
}

The destructor of object "a" is not invoked.

Question 1: Can I force stack unwinding and destruct "a" before
invoking exit()?


Suppose we are in a signal handler:

void sig_handler()
{
exit(3);
}

So, local objects will not be destructed.

Question 2: Is there any way that all local objects will be destructed
when our customized signal handler chooses to terminate the process?
 
J

Juha Nieminen

Jim King said:
A is an RAII class.

This got me curious. What is a "RAII class"? Perhaps more importantly,
what would be a "non-RAII class"?
Question 1: Can I force stack unwinding and destruct "a" before
invoking exit()?

Throw an exception (and catching it on the outermost scope inside
main()) instead of calling exit().
 
J

Johannes Schaub (litb)

Juha said:
Throw an exception (and catching it on the outermost scope inside
main()) instead of calling exit().

It may be worth to note that this is an approach the Standard shows too in a
footnote to [lib.support.start.term]/p8: "Non-local objects with static
storage duration are destroyed in the reverse order of the comple-
tion of their constructor.(Automatic objects are not destroyed as a result
of calling exit().) [footnote: 'Objects with automatic storage duration are
all destroyed in a program whose function main() contains no automatic
objects and executes the call to exit(). Control can be transferred directly
to such a main() by throwing an exception that is caught in main().']"
..
 
J

Juha Nieminen

Johannes Schaub (litb) said:
Juha said:
Throw an exception (and catching it on the outermost scope inside
main()) instead of calling exit().

It may be worth to note that this is an approach the Standard shows too in a
footnote to [lib.support.start.term]/p8: "Non-local objects with static
storage duration are destroyed in the reverse order of the comple-
tion of their constructor.(Automatic objects are not destroyed as a result
of calling exit().) [footnote: 'Objects with automatic storage duration are
all destroyed in a program whose function main() contains no automatic
objects and executes the call to exit(). Control can be transferred directly
to such a main() by throwing an exception that is caught in main().']"

Throwing an exception (which is caught in main()) to exit the program
gracefully instead of calling exit() also makes sense because exception
throwing does not make your program any less efficient or penalize it in
any other way (in the normal case where the exception is not thrown,
that is). There's little reason to not to use exceptions for this.

(There might be some rare situations where you want to compile your
program without support for exceptions. Many C++ compilers have an option
for this, and doing so usually results in a smaller executable binary.
However, this is seldom something which is really necessary, and with
the vast majority of programs you usually don't need to care.)
 
J

Jeff Flinn

Sorry, this info was just from google search result page ;-) (searching
for: signal handler c++ exception)

1.
C++ and Signal Handling
In particular, any use of C++ exception handling within a signal
handler is likely to lead to disaster (and indeed a footnote in the
standard points this ...
www.glenmccl.com/ansi_032.htm - Puhverdatud - Sarnased
2.
C++ exception-handling tricks for Linux
23 Feb 2005 ... Some Linux/UNIX systems require a stack fix-up
before you can throw the C++ exception from within your signal handler.
...
www.ibm.com/.../linux/.../l-cppexcep.html - Puhverdatud - Sarnased

Can you share the full link?

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top