home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: How to run destructors of all objects when failure exit occurs ?
- Message-ID: <5213@holden.lulea.trab.se>
- Date: 16 Nov 92 12:11:33 GMT
- References: <AAAsb0h8xN@inpbox.inp.nsk.su>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 51
- X-Newsreader: Tin 1.1 PL4
-
- dolgov@inpbox.inp.nsk.su (Yuri V. Dolgov) writes:
- :
- : Often I use failure program exit when some conditions are bad.
- : The trouble is: I created some objects (e.g. dialog boxes, network connections
- : and so on) and I want to run destructors before program exit.
- : Is there exist standard technique in C++ language to solve this problem ?
-
- Well, yes, it is called exception handling, and it is in the C++ language,
- although it is not implemented by most current compilers.
-
- _If_ you had an exception-capable compiler, you might say
-
- class DieSucker { }; // Empty class used to represent total program failure.
- throw DieSucker; // causes unconditional branch out of the program,
- // unless a statement "catch( DieSucker )" is
- // somewhere in the call chain.
-
- Destructors will be called for all stack-based (automatic) variables.
- You should represent the program resources as such automatic variables.
-
- Anyway, today you will need to take care of the destruction business
- yourself, by creating a special Resource class, from which the other
- resource classes inherit. The constructor of class Resource registers
- the Resource in a global list. Resource::cleanup() automatically calls
- the destructors of the registered objects.
-
- More details can be found in my paper "C++, Without Exceptions"
- (FTP info below). Section 3.6 discusses the creation of a Resource
- class, which you should be able to use for your purpose, with small
- changes (make the Resource::cleanup() method puclic, so you can
- call it before calling exit()).
-
- ==== FTP info ======================================================
- I have made my paper "C++, without exceptions" available for anonymous FTP.
- Mats Henricsson at Ellemtel Utvecklings AB has helped me with this.
- Thanks Mats!
-
- The document was written using FrameMaker, but has been saved in Postscript
- and compressed. It can be accessed by anonymous ftp from the server:
- euagate.eua.ericsson.se (Internet Address: 134.138.134.16)
- It is in the directory:
- ~ftp/pub/eua/c++
- The file is ~36k large and its name is:
- Except.ps.Z
- =====================================================================
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-