public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Great g++ bug! Local destructor isn't called!
@ 2004-03-10 18:34 Eljay Love-Jensen
  0 siblings, 0 replies; 11+ messages in thread
From: Eljay Love-Jensen @ 2004-03-10 18:34 UTC (permalink / raw)
  To: Moore, Mathew L, gcc-help

Hi Matt,

 >Just out of curiosity, what is the reasoning for this?  Is this for g++ 
specifically, or C++ in general?

It's for C++ in general.

The rational (and experience) is that if someone puts a throw(something) 
specification on a function or method, and that routine relies upon other 
code, and that code is unspecified OR the throw specification changes AND 
the unsuspecting dependent code with the throw specification or try/catch 
wrapper is not updated when the called code changes... k-boom.

In that situation, the program terminates buh-bye sayonara.  The user's 
work is lost, customer service gets lots of calls from irate customers, 
developers get to work 80 hours of emergency crunch time to resolve the bug 
-- all because of throw(something) specifications that do only harm, and do 
no good (since they are not used at compile time).

PLUS, having a throw(something) specification on a function or method 
incurs run-time overhead (!).  Remember the C++ rule, your not supposed to 
pay for something if you don't put it in?  The throw(something) 
specification is checked at runtime, and is something that you pay for in 
performance.

Not that people should be using the C++ exception mechanism for regular 
flow control... but some people do.

How does this happen?  Say, for instance, development is done in two 
different teams that rarely communicate with each other.  Someone misses 
that a header file changed with a new or altered throw(something) 
specification... bug in waiting.

You could program defensively (and maybe that's not a bad idea).  But now 
you MUST enforce that all routines that have a throw(something) 
specification MUST have a try {} catch(...) {} block at the outermost level 
of the body.  And what are you gaining?  You are consuming all exceptions 
pell-nell, cannot handle them (presumably), while somewhere up the stack 
perhaps there was a routine which could have handled the mystery 
exception.  To what benefit...?

More code, less readability, less maintainability, more risk, more chances 
for things going awry IN THE PRODUCTION CODE.  Not good.

--Eljay

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Great g++ bug! Local destructor isn't called!
@ 2004-03-11 21:37 Assinovsky, Lev
  0 siblings, 0 replies; 11+ messages in thread
From: Assinovsky, Lev @ 2004-03-11 21:37 UTC (permalink / raw)
  To: Assinovsky, Lev, Eljay Love-Jensen, gcc-help

Sorry for mistyping in my previous message.
Right text is:
The bug is NOT shown up if 
an exception spec of exception raiser exactly matches 
an exception spec of the virtual function.

----
Lev Assinovsky
Aelita Software Corporation
O&S InTrust Framework Division, Team Leader
ICQ# 165072909


> -----Original Message-----
> From: Assinovsky, Lev 
> Sent: Thursday, March 11, 2004 1:43 PM
> To: Eljay Love-Jensen; gcc-help@gcc.gnu.org
> Subject: RE: Great g++ bug! Local destructor isn't called!
> 
> 
> If to compile with -fno-enforce-eh-specs my testcase works.
> Actually that's very natural. The bug is shown up if 
> an exception spec of exception raiser exactly matches 
> an exception spec of the virtual function.
> 
> ----
> Lev Assinovsky
> Aelita Software Corporation
> O&S InTrust Framework Division, Team Leader
> ICQ# 165072909
> 
> 
> > -----Original Message-----
> > From: Eljay Love-Jensen [mailto:eljay@adobe.com]
> > Sent: Wednesday, March 10, 2004 5:46 PM
> > To: Assinovsky, Lev; gcc-help@gcc.gnu.org
> > Subject: Re: Great g++ bug! Local destructor isn't called!
> > 
> > 
> > Hi Lev,
> > 
> > I notice that if the throw(int) specification is taken off 
> the Raiser 
> > constructor, then the ~Object() is called with -O3.
> > 
> > (I'm using GCC 3.3.1 on CygWin / Windows XP.)
> > 
> > Very odd.  Good catch.  Have you filed a bug?
> > 
> > BTW, in general, I've found that it's usually best NOT to put 
> > in throw 
> > specifications for functions / methods.  Ever.  (This 
> > restriction does not 
> > apply to putting in the "throw() -- I throw nothing, ever" 
> > specification.  But even that should be used with great caution.)
> > 
> > If C++ did exception specifications like how Java does them, 
> > then that'd be 
> > a different story.
> > 
> > --Eljay
> > 
> > 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Great g++ bug! Local destructor isn't called!
@ 2004-03-11 18:20 Assinovsky, Lev
  0 siblings, 0 replies; 11+ messages in thread
From: Assinovsky, Lev @ 2004-03-11 18:20 UTC (permalink / raw)
  To: Eljay Love-Jensen, gcc-help

If to compile with -fno-enforce-eh-specs my testcase works.
Actually that's very natural. The bug is shown up if 
an exception spec of exception raiser exactly matches 
an exception spec of the virtual function.

----
Lev Assinovsky
Aelita Software Corporation
O&S InTrust Framework Division, Team Leader
ICQ# 165072909


> -----Original Message-----
> From: Eljay Love-Jensen [mailto:eljay@adobe.com]
> Sent: Wednesday, March 10, 2004 5:46 PM
> To: Assinovsky, Lev; gcc-help@gcc.gnu.org
> Subject: Re: Great g++ bug! Local destructor isn't called!
> 
> 
> Hi Lev,
> 
> I notice that if the throw(int) specification is taken off the Raiser 
> constructor, then the ~Object() is called with -O3.
> 
> (I'm using GCC 3.3.1 on CygWin / Windows XP.)
> 
> Very odd.  Good catch.  Have you filed a bug?
> 
> BTW, in general, I've found that it's usually best NOT to put 
> in throw 
> specifications for functions / methods.  Ever.  (This 
> restriction does not 
> apply to putting in the "throw() -- I throw nothing, ever" 
> specification.  But even that should be used with great caution.)
> 
> If C++ did exception specifications like how Java does them, 
> then that'd be 
> a different story.
> 
> --Eljay
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Great g++ bug! Local destructor isn't called!
@ 2004-03-10 19:49 Martin York
  0 siblings, 0 replies; 11+ messages in thread
From: Martin York @ 2004-03-10 19:49 UTC (permalink / raw)
  To: gcc-help

 Hi Matt,

 >Just out of curiosity, what is the reasoning for this?  Is this for
g++ specifically, or C++ in general?

It's for C++ in general.

<SNIP>

--Eljay


Scott Meyers Book "More Effective C++" covers this topic in detail. It's
also a very good read in general.

<<http://www.awprofessional.com/isapi/product_id~{3923C66A-8589-44A3-8A8
7-926854646935}/selectDescTypeId~{06B328CA-921B-4395-945D-3078CA6F292A}/
st~{75A0BC87-B9B4-435A-86E0-36F0AC8E0923}/session_id~{6EB35E33-F9B3-4270
-9DC8-C9E6136F4D86}/catalog/product.asp>>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Great g++ bug! Local destructor isn't called!
  2004-03-10 16:02 Moore, Mathew L
@ 2004-03-10 16:49 ` Momchil Velikov
  0 siblings, 0 replies; 11+ messages in thread
From: Momchil Velikov @ 2004-03-10 16:49 UTC (permalink / raw)
  To: Moore, Mathew L; +Cc: Eljay Love-Jensen, gcc-help

>>>>> "Moore" == Moore, Mathew L <MooreML@BATTELLE.ORG> writes:

Moore> <snip>
>> BTW, in general, I've found that it's usually best NOT to put 
>> in throw 
>> specifications for functions / methods.  Ever.  (This 
>> restriction does not 
>> apply to putting in the "throw() -- I throw nothing, ever" 
>> specification.  But even that should be used with great caution.)
>> 

Moore> Just out of curiosity, what is the reasoning for this?  Is this for g++
Moore> specifically, or C++ in general?

  It must intercept thrown exceptiions and call ``unexpected()'' if
they do not match the specification.  Pure overhead.

  Including ``throw()''.  

~velco

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Great g++ bug! Local destructor isn't called!
@ 2004-03-10 16:02 Moore, Mathew L
  2004-03-10 16:49 ` Momchil Velikov
  0 siblings, 1 reply; 11+ messages in thread
From: Moore, Mathew L @ 2004-03-10 16:02 UTC (permalink / raw)
  To: Eljay Love-Jensen, gcc-help

<snip>

> BTW, in general, I've found that it's usually best NOT to put 
> in throw 
> specifications for functions / methods.  Ever.  (This 
> restriction does not 
> apply to putting in the "throw() -- I throw nothing, ever" 
> specification.  But even that should be used with great caution.)
> 


Just out of curiosity, what is the reasoning for this?  Is this for g++
specifically, or C++ in general?

Thanks,
--Matt



> If C++ did exception specifications like how Java does them, 
> then that'd be 
> a different story.
> 
> --Eljay
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Great g++ bug! Local destructor isn't called!
@ 2004-03-10 15:59 Assinovsky, Lev
  0 siblings, 0 replies; 11+ messages in thread
From: Assinovsky, Lev @ 2004-03-10 15:59 UTC (permalink / raw)
  To: Tony Wetmore, Eljay Love-Jensen, gcc-help

Five conditions must be true to get that bug:
1. Method throwing exception must have an exception specifier.
2-4. Method calling the method above must be VIRTUAL, must not  have an exception specifier and
     must be called through base class pointer or reference.
5. Compile with option -On,  n > 0.

----
Lev Assinovsky
Aelita Software Corporation
O&S InTrust Framework Division, Team Leader
ICQ# 165072909


> -----Original Message-----
> From: Tony Wetmore [mailto:tony.wetmore@solipsys.com]
> Sent: Wednesday, March 10, 2004 6:23 PM
> To: 'Eljay Love-Jensen'; Assinovsky, Lev; gcc-help@gcc.gnu.org
> Subject: RE: Great g++ bug! Local destructor isn't called!
> 
> 
> Interestingly, ~Object() is also called if you ADD a "throw(int)"
> specifier to the FromBase::Run() method that invokes the Raiser
> constructor.  Tested on Linux with GCC 3.3.1.
> 
> ---
> Tony Wetmore
> Raytheon Solipsys
> mailto:tony.wetmore@solipsys.com
> http://www.solipsys.com
>  
>  
> 
> 
> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org 
> [mailto:gcc-help-owner@gcc.gnu.org] On
> Behalf Of Eljay Love-Jensen
> Sent: Wednesday, March 10, 2004 9:46 AM
> To: Assinovsky, Lev; gcc-help@gcc.gnu.org
> Subject: Re: Great g++ bug! Local destructor isn't called!
> 
> 
> Hi Lev,
> 
> I notice that if the throw(int) specification is taken off the Raiser 
> constructor, then the ~Object() is called with -O3.
> 
> (I'm using GCC 3.3.1 on CygWin / Windows XP.)
> 
> Very odd.  Good catch.  Have you filed a bug?
> 
> BTW, in general, I've found that it's usually best NOT to put 
> in throw 
> specifications for functions / methods.  Ever.  (This restriction does
> not 
> apply to putting in the "throw() -- I throw nothing, ever" 
> specification.  But even that should be used with great caution.)
> 
> If C++ did exception specifications like how Java does them, 
> then that'd
> be 
> a different story.
> 
> --Eljay
> 
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Great g++ bug! Local destructor isn't called!
  2004-03-10 15:23 ` Eljay Love-Jensen
@ 2004-03-10 15:57   ` Tony Wetmore
  0 siblings, 0 replies; 11+ messages in thread
From: Tony Wetmore @ 2004-03-10 15:57 UTC (permalink / raw)
  To: 'Eljay Love-Jensen', 'Assinovsky, Lev', gcc-help

Interestingly, ~Object() is also called if you ADD a "throw(int)"
specifier to the FromBase::Run() method that invokes the Raiser
constructor.  Tested on Linux with GCC 3.3.1.

---
Tony Wetmore
Raytheon Solipsys
mailto:tony.wetmore@solipsys.com
http://www.solipsys.com
 
 


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Eljay Love-Jensen
Sent: Wednesday, March 10, 2004 9:46 AM
To: Assinovsky, Lev; gcc-help@gcc.gnu.org
Subject: Re: Great g++ bug! Local destructor isn't called!


Hi Lev,

I notice that if the throw(int) specification is taken off the Raiser 
constructor, then the ~Object() is called with -O3.

(I'm using GCC 3.3.1 on CygWin / Windows XP.)

Very odd.  Good catch.  Have you filed a bug?

BTW, in general, I've found that it's usually best NOT to put in throw 
specifications for functions / methods.  Ever.  (This restriction does
not 
apply to putting in the "throw() -- I throw nothing, ever" 
specification.  But even that should be used with great caution.)

If C++ did exception specifications like how Java does them, then that'd
be 
a different story.

--Eljay


^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Great g++ bug! Local destructor isn't called!
@ 2004-03-10 15:38 Assinovsky, Lev
  0 siblings, 0 replies; 11+ messages in thread
From: Assinovsky, Lev @ 2004-03-10 15:38 UTC (permalink / raw)
  To: Eljay Love-Jensen, gcc-help



> -----Original Message-----
> From: Eljay Love-Jensen [mailto:eljay@adobe.com]
> Sent: Wednesday, March 10, 2004 5:46 PM
> To: Assinovsky, Lev; gcc-help@gcc.gnu.org
> Subject: Re: Great g++ bug! Local destructor isn't called!
> 
> 
> Hi Lev,
> 
> I notice that if the throw(int) specification is taken off the Raiser 
> constructor, then the ~Object() is called with -O3.
> 
> (I'm using GCC 3.3.1 on CygWin / Windows XP.)
> 
> Very odd.  Good catch.  Have you filed a bug?

Not yet.

> 
> BTW, in general, I've found that it's usually best NOT to put 
> in throw 
> specifications for functions / methods.  Ever. 

Too late!! We have tons of code with exception specification!

 (This 
> restriction does not 
> apply to putting in the "throw() -- I throw nothing, ever" 
> specification.  But even that should be used with great caution.)
> 
> If C++ did exception specifications like how Java does them, 
> then that'd be 
> a different story.
> 
> --Eljay
> 
> 


----
Lev Assinovsky
Aelita Software Corporation
O&S InTrust Framework Division, Team Leader
ICQ# 165072909

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Great g++ bug! Local destructor isn't called!
  2004-03-10 14:52 Assinovsky, Lev
@ 2004-03-10 15:23 ` Eljay Love-Jensen
  2004-03-10 15:57   ` Tony Wetmore
  0 siblings, 1 reply; 11+ messages in thread
From: Eljay Love-Jensen @ 2004-03-10 15:23 UTC (permalink / raw)
  To: Assinovsky, Lev, gcc-help

Hi Lev,

I notice that if the throw(int) specification is taken off the Raiser 
constructor, then the ~Object() is called with -O3.

(I'm using GCC 3.3.1 on CygWin / Windows XP.)

Very odd.  Good catch.  Have you filed a bug?

BTW, in general, I've found that it's usually best NOT to put in throw 
specifications for functions / methods.  Ever.  (This restriction does not 
apply to putting in the "throw() -- I throw nothing, ever" 
specification.  But even that should be used with great caution.)

If C++ did exception specifications like how Java does them, then that'd be 
a different story.

--Eljay

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Great g++ bug! Local destructor isn't called!
@ 2004-03-10 14:52 Assinovsky, Lev
  2004-03-10 15:23 ` Eljay Love-Jensen
  0 siblings, 1 reply; 11+ messages in thread
From: Assinovsky, Lev @ 2004-03-10 14:52 UTC (permalink / raw)
  To: gcc-help

Hi folks!
In the example bellow the destructor  of Object isn't called when exception
was raised from Run() method! That happens only with option -O1, -O2, -O3.
-O0 works fine.
Tested with gcc 3.2 and 3.3.1 in Solaris 8 Intel and HP-UX 11.00.

================== cut here =====================

#include <iostream>

     
class Raiser
{
public:
    Raiser()  throw( int )
    {
        throw 1;
    };
};

    
class Object
{
public:
    ~Object()
    {
        std::cout << "~Object()" << std::endl;
    };
};


class Base
{
public:
    virtual ~Base(){};
    virtual void Run(){};
};


class FromBase : public Base
{
public:
    virtual ~FromBase(){};
    virtual void Run()
    {
        std::cout << "Derived Run" << std::endl;
        {
            Object a;
            std::cout << "Raise!" << std::endl;
            Raiser   riser;
        }
        std::cout << "Unreachable code" << std::endl;
    };
};

int main()
{
    FromBase a;
    Base& b = static_cast<Base&>(a);
    
    try
    {
        b.Run();
        std::cout << "Unreach 2" << std::endl;
    }
    catch ( int ) 
    {
        std::cout << "Exception handler" << std::endl;
    }
    
    std::cout << "Exit Main" << std::endl;
    return 0;
}

================ cut here =======================

Also we discovered calling a free function throwing an exception leads to same  bug appears with -O3 only.
In general this bug coming  when and only when class methods or free functions have exception specification!

Any help will be greatly appreciated!

----
Lev Assinovsky
Aelita Software Corporation
O&S InTrust Framework Division

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-03-11 11:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-10 18:34 Great g++ bug! Local destructor isn't called! Eljay Love-Jensen
  -- strict thread matches above, loose matches on Subject: below --
2004-03-11 21:37 Assinovsky, Lev
2004-03-11 18:20 Assinovsky, Lev
2004-03-10 19:49 Martin York
2004-03-10 16:02 Moore, Mathew L
2004-03-10 16:49 ` Momchil Velikov
2004-03-10 15:59 Assinovsky, Lev
2004-03-10 15:38 Assinovsky, Lev
2004-03-10 14:52 Assinovsky, Lev
2004-03-10 15:23 ` Eljay Love-Jensen
2004-03-10 15:57   ` Tony Wetmore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).