Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!swrinde!mips!spool.mu.edu!agate!forney.berkeley.edu!jbuck From: jbuck@forney.berkeley.edu (Joe Buck) Newsgroups: comp.lang.c++ Subject: Re: How do I call destructors on ^C in TC++? Message-ID: <1991May29.032559.10858@agate.berkeley.edu> Date: 29 May 91 03:25:59 GMT References: <1991May24.045847.24401@colorado.edu> <1991May27.024810.2300@minyos.xx.rmit.oz.au> <1991May28.225141.1451@mathcs.sjsu.edu> Sender: root@agate.berkeley.edu (Charlie Root) Reply-To: jbuck@forney.berkeley.edu (Joe Buck) Organization: University of California, Berkeley Lines: 48 In article <1991May28.225141.1451@mathcs.sjsu.edu>, horstman@mathcs.sjsu.edu (Cay Horstmann) writes: |> In article <1991May27.024810.2300@minyos.xx.rmit.oz.au> s892992@minyos.xx.rmit.oz.au (Kendall Bennett) writes: |> > I am pretty sure that if you call the exit() standard library function it |> >calls the destructors for any AUTO variables, that is any instances that have |> >been allocated on the stack. If the class instance was allocted in the heap, |> >then it is up to the program to delete it - so I don't know how you would |> >handle this one. |> > |> Fat chance. No such thing will happen. When you call exit(), the stack |> just gets popped, the free store given back wholesale. I am not sure whether |> STATIC destructors are run. Someone here surely knows the answer. Cay's right; there's no provision in Turbo C++ or or cfront up through 2.1 that would allow you to have a control-C interrupt call destructors for the auto variables. When exceptions become commonly available, there will be a solution: the control-C signal handler can throw an exception, and if you want to catch it at the top level and exit, you can write your main() something like #include class IntSignal { // stuff goes here }; void intCatcher() throw IntSignal { throw IntSignal(); } int main (int argc, char ** argv) { // I'm being sloppy, should check the current status // of signal handling. signal (SIGINT, intCatcher); try { // body of main goes here } catch (IntSignal& isig) { cerr << "Caught a SIGINT, exiting\n"; exit(1); } } The exception handling mechanism will cause all auto values on the stack to have their destructors called. -- Joe Buck jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck