home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16403 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  2.3 KB

  1. Path: sparky!uunet!wupost!sdd.hp.com!spool.mu.edu!agate!doc.ic.ac.uk!uknet!mcsun!sunic!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to run destructors of all objects when failure exit occurs ?
  5. Message-ID: <5217@holden.lulea.trab.se>
  6. Date: 16 Nov 92 22:31:28 GMT
  7. References: <BxsFJE.6GL@news.cso.uiuc.edu>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 38
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. pl2440@meibm31.cen.uiuc.edu (Paul Jay Lucas) writes:
  13. : In <AAAsb0h8xN@inpbox.inp.nsk.su> dolgov@inpbox.inp.nsk.su (Yuri V. Dolgov) writes:
  14. : >   Often I use failure program exit when some conditions are bad.
  15. : >The trouble is: I created some objects (e.g. dialog boxes, network connections
  16. : >and so on) and I want to run destructors before program exit.
  17. : >Is there exist standard technique in C++ language to solve this problem ?
  18. :     No; only statically-allocated objects are destroyed.  Off the
  19. :     top of my head, *a* way to have auto-objects destroyed would be
  20. :     to make them register themselves with some global list of
  21. :     objects on construction and deregister themselves on
  22. :     destruction.  Now, when the global "clean-up" object gets
  23. :     destroyed, it could destroy all objects that are still
  24. :     registered.
  25.  
  26. Don't do that.  The memory occupied by those auto objects may have been
  27. resused at this point, at least I imagine this might happen.  This is an
  28. interesting issue; is it guaranteed that global destructors are called
  29. while "inside" the exit call, or does this happen "after" exit() is
  30. done?
  31.  
  32. :     Actually, they can't be auto objects, but must instead be
  33. :     dynamically-allocated objects; it is illegal to delete anything
  34. :     not allocated by new (as auto objects aren't).
  35.  
  36. No problem.  Executing the destructors is enough, no need to
  37. call "delete".  Using "auto" objects for these things should be
  38. encouraged (it will be required when we finally get exceptions).
  39.  
  40. As I said above, I think it is safest to require an explicit cleanup
  41. call before calling exit().
  42.  
  43. -- 
  44. --------------------------------------------------------------------------
  45. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  46. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  47. --------------------------------------------------------------------------
  48.