Topic: Exceptions and DTORS of thrown objects
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Thu, 3 Mar 1994 16:01:22 GMT Raw View
djones@megatest.com (Dave Jones) writes:
>What does the C++ standard say about throwing objects
>that have destructors? Anything?
No, nothing, since there's no C++ standard yet.
But if you want to know what the ARM says, read on.
>Is it legal to throw an object that has a destructor?
Yes.
>Using SUNSWpro C++ release 3.0, I tried a toy example, and
>it does not seem to work, although the compiler does
>not complain a bit.
Your compiler is broken.
>Apparently the throw/catch mechanism uses the
>copy-constructor. That's okay with me, but there are
>three constructor calls, and only two destructor calls.
>Why would it call the copy-constructor more than once?
The ARM states that the thrown object is copied to a
temporary, and then that temporary is used to
initialize the catch parameter. So three constructor
calls is correct. The problem is that the compiler
is then supposed to destroy both the catch parameter
and the temporary before exiting the handler,
and your compiler seems to be failing to do this.
I suggest that you file a bug report.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: rych@festival.ed.ac.uk (R Hawkes)
Date: Sat, 26 Feb 1994 13:31:11 GMT Raw View
djones@megatest.com (Dave Jones) writes:
>What does the C++ standard say about throwing objects
>that have destructors? Anything? Is it legal to throw
>an object that has a destructor?
>Using SUNSWpro C++ release 3.0, I tried a toy example, and
>it does not seem to work, although the compiler does
>not complain a bit.
Quoting from the Ref. Manual in BS's C++ Prog. Lang. r.15.7 "An object may
be thrown if it can be copied and destroyed in the context of the function
in which the throw occurs."
>Apparently the throw/catch mechanism uses the
>copy-constructor. That's okay with me, but there are
>three constructor calls, and only two destructor calls.
>Why would it call the copy-constructor more than once?
>What's the deal? What is the deal supposed to be?
>The program below produces this output:
> Except CTOR Exception stuff
> Except copy CTOR Exception stuff
> Except copy CTOR Exception stuff
> caught Exception stuff
> ~Except DTOR Exception stuff
> ~Except DTOR Exception stuff
>
>Moving the declaration of the exception object
>into the block that throws it only makes matters worse: A
>constructor is then called three times, but the destructor only
>once.
>
(Quoting again:) The throw expression initialises a temporary object of the
static type of the operand of throw() and uses that temporary to initialise
the ... variable in the handler. That might explain the two copy CTORs, the
first for the temporary object and the second for the actual object "e".
I've just working out exceptions and unfortunately I don't have a clue
why you would only see one DTOR call, maybe the compiler treats temporary
objects differently??
Instead of:
> }catch(Except e) {
> cerr << "caught " << e.ename() << endl;
> }
...try (no pun intended):
}catch(Except& e) {
cerr << "caught " << e.ename() << endl;
}
Now the copy constructor gets called just the once and the destructor is
called twice. If you move the declaration of the object into the block
that throws it things still balance out. Does "e" now refer to the temporary
object or what??
Anybody else got an idea?
Rych
======================================================================
Rycharde Hawkes email: rych@festival.ed.ac.uk
Virtual Environment Laboratory
Dept. of Psychology Tel : +44 31 650 3426
Univ. of Edinburgh Fax : +44 31 650 6534
======================================================================