home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16349 < prev    next >
Encoding:
Text File  |  1992-11-16  |  2.5 KB  |  51 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!pipex!warwick!bham!bhamvx!mccauleyba
  3. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  4. Subject: Re: How to run destructors of all objects when failure exit occurs ?
  5. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  6. Message-ID: <1992Nov16.131839.1@vax1.bham.ac.uk>
  7. Date: Mon, 16 Nov 1992 13:18:39 GMT
  8. Lines: 39
  9. References: <AAAsb0h8xN@inpbox.inp.nsk.su> <BxsFJE.6GL@news.cso.uiuc.edu>
  10. Organization: University of Birmingham
  11.  
  12. In article <BxsFJE.6GL@news.cso.uiuc.edu>, 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. >     Actually, they can't be auto objects, but must instead be
  26. >     dynamically-allocated objects; it is illegal to delete anything
  27. >     not allocated by new (as auto objects aren't).
  28. If you use exception handling rather than exit() then all your `auto's are
  29. destroyed neatly. To get rid of dynamics either have a global list (as above)
  30. or if you prefer an abitary number of `auto' "clean-up" objects each of
  31. which cleans up objects in a given scope. See the section in "The C++
  32. Prog.Lang.Edn.2" on exceptions for some examples of this.
  33.  
  34. #define exit(i) throw int(i)
  35. int main() { try {/* program body */} catch (int retval) { return retval; } }
  36.  
  37. Actually I wouldn't `#define exit' in reality but it shows the idea
  38.  
  39. If, like me, your compiler does not yet have exception handling :-( then lobby
  40. your compiler implementor.
  41. -- 
  42.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  43.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  44. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  45. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  46.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  47. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  48.