home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!sdd.hp.com!spool.mu.edu!agate!doc.ic.ac.uk!uknet!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: <5217@holden.lulea.trab.se>
- Date: 16 Nov 92 22:31:28 GMT
- References: <BxsFJE.6GL@news.cso.uiuc.edu>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 38
- X-Newsreader: Tin 1.1 PL4
-
- pl2440@meibm31.cen.uiuc.edu (Paul Jay Lucas) writes:
- : In <AAAsb0h8xN@inpbox.inp.nsk.su> 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 ?
- :
- : No; only statically-allocated objects are destroyed. Off the
- : top of my head, *a* way to have auto-objects destroyed would be
- : to make them register themselves with some global list of
- : objects on construction and deregister themselves on
- : destruction. Now, when the global "clean-up" object gets
- : destroyed, it could destroy all objects that are still
- : registered.
-
- Don't do that. The memory occupied by those auto objects may have been
- resused at this point, at least I imagine this might happen. This is an
- interesting issue; is it guaranteed that global destructors are called
- while "inside" the exit call, or does this happen "after" exit() is
- done?
-
- : Actually, they can't be auto objects, but must instead be
- : dynamically-allocated objects; it is illegal to delete anything
- : not allocated by new (as auto objects aren't).
-
- No problem. Executing the destructors is enough, no need to
- call "delete". Using "auto" objects for these things should be
- encouraged (it will be required when we finally get exceptions).
-
- As I said above, I think it is safest to require an explicit cleanup
- call before calling exit().
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-