This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: gcc-3.0 bug - destructor not called when throwing an exception


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]