home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13296 < prev    next >
Encoding:
Text File  |  1992-09-04  |  2.1 KB  |  58 lines

  1. Path: sparky!uunet!UB.com!igor!thor!rmartin
  2. From: rmartin@thor.Rational.COM (Bob Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Destructors and exit()
  5. Message-ID: <rmartin.715615353@thor>
  6. Date: 4 Sep 92 14:02:33 GMT
  7. References: <1992Sep3.220301.16983@murdoch.acc.Virginia.EDU>
  8. Sender: news@Rational.COM
  9. Lines: 47
  10.  
  11. rad2r@uvacs.cs.Virginia.EDU (Robert DeLine) writes:
  12.  
  13.  
  14. |Sorry for this novice question, but when I call exit() from a program compiled
  15. |using g++ 2.1, the destructors for my objects don't get called.  This doesn't
  16. |surprise me too much, but is this correct?  Does the ARM mention what should
  17. |happen when exit() is called?  What if the program is ended due to an uncaught
  18. |exception?  If destructors _are_ called when leaving via an uncaught exception,
  19. |perhaps this is a better way than calling exit() to leave from deep inside a
  20. |program?
  21.  
  22. The destructors for static objects should be invoked when you call
  23. exit.  However the destructors for objects which are declared as
  24. 'auto' variables (on the stack) in the function which calls exit,
  25. probably will not get invoked.  For example.
  26.  
  27. main()
  28. {
  29.   MyClass A;
  30.   static MyClass B;
  31.  
  32.   exit();
  33. }
  34.  
  35.  
  36. The destructor for B should be invoked.  But the destructor for A
  37. probably will not.  I say probably, because a clever compiler might be
  38. able to figure out that, upon exit, the enclosing stack frames should
  39. be destroyed.
  40.  
  41. The definition of the exception feature says that all destructors for
  42. objects created as auto variables between the throw and the catch will
  43. be invoked.  Thus, "exit" does not behave the way an exception would.
  44. Presumably, an uncaught exception will be "caught" by the code that
  45. calls main.  Thus, destructors will most likely be invoked for
  46. uncaught exceptions.
  47.  
  48. I agree, when exceptions are implemented, the present a better
  49. mechanisms for exitting a program than embedded calls to 'exit' do.
  50. And remember, never use gotos.
  51.  
  52.  
  53. --
  54. Robert Martin                        Training courses offered in:
  55. R. C. M. Consulting                       Object Oriented Analysis
  56. 2080 Cranbrook Rd.                        Object Oriented Design
  57. Green Oaks, Il 60048 (708) 918-1004       C++
  58.