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

  1. Path: sparky!uunet!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: <5213@holden.lulea.trab.se>
  6. Date: 16 Nov 92 12:11:33 GMT
  7. References: <AAAsb0h8xN@inpbox.inp.nsk.su>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 51
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. dolgov@inpbox.inp.nsk.su (Yuri V. Dolgov) writes:
  13. :    Often I use failure program exit when some conditions are bad.
  14. : The trouble is: I created some objects (e.g. dialog boxes, network connections
  15. : and so on) and I want to run destructors before program exit.
  16. : Is there exist standard technique in C++ language to solve this problem ?
  17.  
  18. Well, yes, it is called exception handling, and it is in the C++ language,
  19. although it is not implemented by most current compilers.
  20.  
  21. _If_ you had an exception-capable compiler, you might say
  22.  
  23. class DieSucker { }; // Empty class used to represent total program failure.
  24. throw DieSucker;    // causes unconditional branch out of the program,
  25.             // unless a statement "catch( DieSucker )" is
  26.             // somewhere in the call chain.
  27.  
  28. Destructors will be called for all stack-based (automatic) variables.
  29. You should represent the program resources as such automatic variables.
  30.  
  31. Anyway, today you will need to take care of the destruction business
  32. yourself, by creating a special Resource class, from which the other
  33. resource classes inherit.  The constructor of class Resource registers
  34. the Resource in a global list.  Resource::cleanup() automatically calls
  35. the destructors of the registered objects.
  36.  
  37. More details can be found in my paper "C++, Without Exceptions"
  38. (FTP info below).  Section 3.6 discusses the creation of a Resource
  39. class, which you should be able to use for your purpose, with small
  40. changes (make the Resource::cleanup() method puclic, so you can
  41. call it before calling exit()).
  42.  
  43. ==== FTP info ======================================================
  44. I have made my paper "C++, without exceptions" available for anonymous FTP.
  45. Mats Henricsson at Ellemtel Utvecklings AB has helped me with this.
  46. Thanks Mats!
  47.  
  48. The document was written using FrameMaker, but has been saved in Postscript
  49. and compressed. It can be accessed by anonymous ftp from the server:
  50.    euagate.eua.ericsson.se   (Internet Address: 134.138.134.16)
  51. It is in the directory:
  52.    ~ftp/pub/eua/c++
  53. The file is ~36k large and its name is:
  54.    Except.ps.Z
  55. =====================================================================
  56.  
  57. -- 
  58. --------------------------------------------------------------------------
  59. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  60. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  61. --------------------------------------------------------------------------
  62.