This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: gcc-3.0 bug - destructor not called when throwing an exception
- To: Ryszard Kabatek <Ryszard dot Kabatek at softax dot pl>
- Subject: Re: gcc-3.0 bug - destructor not called when throwing an exception
- From: Phil Edwards <pedwards at disaster dot jaj dot com>
- Date: Fri, 27 Jul 2001 13:27:55 -0400
- Cc: gcc-bugs at gcc dot gnu dot org
- References: <3B616112.3B617086@softax.pl>
On Fri, Jul 27, 2001 at 02:39:46PM +0200, Ryszard Kabatek wrote:
> I saw the message below on a mailing list, perhaps it was reported to gcc-bugs.
I didn't see it until now, so probably not. Would you please be so kind
as to forward my response back to the mailing list in question?
> void f() {
> A a;
>
> try {
> throw long(0);
> }
> catch(int&) {
> cerr << "Got int exception" << endl;
> }
> }
[...]
> -------------
> Compile the test code with gcc-3.0 on linux. Execute the program will
> produce the following
> output:
>
> $ ./testcc
> A()
> Got long 0
>
> The correct output is:
> $ ./testcc
> A()
> ~A()
> Got long 0
No, GCC is correct here, I believe. Let's go to the C++ standard:
# [15.2]/1
# As control passes from a throw-expression to a handler, destructors
# are invoked for all automatic objects constructed since the try block
# was entered.
The ctor for 'a' finished before the try block was entered. Automatic
unwinding only affects the contents of the try block. (If I am mistaken
here, please cite the relevent clause from the standard.)
Moving the definition of 'a' inside the try block
try {
A a;
throw long(0);
}
yields your expected results using GCC 3.0 (under Linux, as it happens):
% ./testcc
A()
~A()
Got long 0
%
Luck++;
Phil
--
Would I had phrases that are not known, utterances that are strange, in
new language that has not been used, free from repetition, not an utterance
which has grown stale, which men of old have spoken.
- anonymous Egyptian scribe, c.1700 BC