home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!UB.com!igor!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: Destructors and exit()
- Message-ID: <rmartin.715615353@thor>
- Date: 4 Sep 92 14:02:33 GMT
- References: <1992Sep3.220301.16983@murdoch.acc.Virginia.EDU>
- Sender: news@Rational.COM
- Lines: 47
-
- rad2r@uvacs.cs.Virginia.EDU (Robert DeLine) writes:
-
-
- |Sorry for this novice question, but when I call exit() from a program compiled
- |using g++ 2.1, the destructors for my objects don't get called. This doesn't
- |surprise me too much, but is this correct? Does the ARM mention what should
- |happen when exit() is called? What if the program is ended due to an uncaught
- |exception? If destructors _are_ called when leaving via an uncaught exception,
- |perhaps this is a better way than calling exit() to leave from deep inside a
- |program?
-
- The destructors for static objects should be invoked when you call
- exit. However the destructors for objects which are declared as
- 'auto' variables (on the stack) in the function which calls exit,
- probably will not get invoked. For example.
-
- main()
- {
- MyClass A;
- static MyClass B;
-
- exit();
- }
-
-
- The destructor for B should be invoked. But the destructor for A
- probably will not. I say probably, because a clever compiler might be
- able to figure out that, upon exit, the enclosing stack frames should
- be destroyed.
-
- The definition of the exception feature says that all destructors for
- objects created as auto variables between the throw and the catch will
- be invoked. Thus, "exit" does not behave the way an exception would.
- Presumably, an uncaught exception will be "caught" by the code that
- calls main. Thus, destructors will most likely be invoked for
- uncaught exceptions.
-
- I agree, when exceptions are implemented, the present a better
- mechanisms for exitting a program than embedded calls to 'exit' do.
- And remember, never use gotos.
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-