This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
c++/1635: 2 constructors and 3 destructors called for thrown object
- To: gcc-gnats at gcc dot gnu dot org
- Subject: c++/1635: 2 constructors and 3 destructors called for thrown object
- From: ouej at cognicase dot ca
- Date: 12 Jan 2001 20:13:41 -0000
- Cc: jean_paul dot ouellet at videotron dot ca
- Reply-To: ouej at cognicase dot ca
>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: