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]

c++/1635: 2 constructors and 3 destructors called for thrown object



>Number:         1635
>Category:       c++
>Synopsis:       2 constructors and 3 destructors called for thrown object
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jan 12 12:16:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Jean-Paul Ouellet
>Release:        gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
>Organization:
>Environment:

>Description:
The sample code simply throws an exception and trace all
constructors (default and copy), the destructor and a
few other calls on cerr. This is a sample output
(the numbers are the object addresses).

create   3221224208
op <<    3221224208
copy     134521840
delete   3221224212
delete   3221224208
catch    134521840
delete   134521840

We see that an object is destructed at address ...212 
without having been created.
>How-To-Repeat:
The following code is simply compiled with, for example,

  g++ -o p p.cpp

** Source code **

#include <iostream.h>


class E
{
  public:
    E() { cerr << "create " << (unsigned int)(this) << endl; }
    E(E &) { cerr << "copy   " << (unsigned int)(this) << endl; }
   ~E() { cerr << "delete " << (unsigned int)(this) << endl; }
  	
    E &operator<<(int) { cerr << "op <<  " << (unsigned int)(this) << endl; return *this; }
};

main()
{
    try
    {
        throw E() << 1;
    }
    catch(E &e)
    {
        cerr << "catch  " << (unsigned int)(&e) << endl;
    }
}
>Fix:
The problem does not occur if the following is used instead
of the one-line throw:

    E e;
    e << 1;
    throw e;
>Release-Note:
>Audit-Trail:
>Unformatted:

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