c++/3282: Problems with throw and destuctor callings

Bohdan Vlasyuk bohdan@kivc.vstu.vinnica.ua
Wed Jun 20 01:26:00 GMT 2001


>Number:         3282
>Category:       c++
>Synopsis:       Problems with throw and destuctor callings
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Wed Jun 20 01:26:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Bohdan Vlasyuk
>Release:        3.0
>Organization:
VSTU
>Environment:
System: Linux kivc.vstu.vinnica.ua 2.4.5 #12 ÷ÔÒ þÅÒ 19 10:01:01 EEST 2001 i686 unknown
Architecture: i686

	
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../srcdir/configure --enable-threads --enable-languages=c,c++ --disable-libgcj
>Description:

When I want to throw class, which has other class inside, and
constructor copies some properties of given class -- it look like
destructors are called wrond. I guess code will explain it better:

--------------------------------------

#include <iostream.h>
int global;

class One {
public:
        int num;
        One() { num=++global; cout << "One " <<num<<endl; };
        void Copy(One second_one) { cout << "Copy " <<num<<endl; };
        ~One() { cout << "~One "<<num<<endl; };
};

class Two {
public:
        One my_one;
        Two(One one) { my_one.Copy(one); };
};


int main() 
{
        One a;
        try {
                throw Two(a);
        } catch (Two two) {
                cout << "Ok"<<endl;
        }
        return 0;       
}


--------------------------------------

It produces following output:
( g++ _test.cpp -o test )


11:12:39 ~/src/eval/src # ./test 
One 1
One 2
Copy 2
~One 1
~One 1
~One 1
~One 1
~One 2
Ok
~One 2
~One 2
~One 1


You see, each destructor is called way more tnat once.


P.s.: It work in a same way with 2.95.3

As a side note: 3.0's g++ appears to be MUCH MUCH slower than 2.95.3

	
>How-To-Repeat:

Preprocessed file is ~600kb large.

Just take file from the above, and compile it with

g++ _test.cpp -o test


	
>Fix:

No ideas.
	
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list