bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A problem about exception in the class's constructor when initializing a


From: jilly
Subject: A problem about exception in the class's constructor when initializing a object array with a initializing member list
Date: 7 Nov 2001 02:20:44 -0800

<Problem name = "A problem about exception in the class's constructor
when
initializing a object array with a initializing member list" 
submitedby="A linux lover">
<Abstract>
         In a C++ code piece,I use a member list to initialize an object(
    object's class name is TestD) array  and complie , link,  the
program
    using g++ in LINUX OS. Specially,I put a exception handling code
block
    in the class TestD's constructor and add a "throw 2" statement by
hand!
    when I comment the "throw 2" statement,the program's result is
right.
    But when i uncomment the "throw 2" statement,the result confuse me
and
    i think it is wrong.Then i compile and link the program in VC++6.0
in
    window2000 OS.After running the program when uncommenting the
"throw 2"
    statement,I find the result is reasonable!(The code and the
program
    result follow)
        
         When I don't use initializing member list , then the program
result
    is right in LINUX OS.
</Abstract>
<CPPCODE>
#include<iostream.h>

class testB
{
public:
    testB(int x=0):_x(x){cout << "call testB(int=0):"<< this << endl;}
    virtual ~testB(){
        cout << "call ~testB:" << this << endl; 
        }
private:
    int _x;
};

class testD:public testB
{
public:
    testD(int x=0){
        try{
            ia = new int[2];        
            throw 2;        
        }catch(int ex){
            delete[] ia;
//<FocusPoint description = "the problem trigger point"
operations="comment or uncomment">
           //throw;
//</FocusPoint>
        }
        cout << "call testD()" << endl; 
    }
    virtual ~testD(){ 
        cout<< "call ~testD:" << this << endl;
        delete []ia;
        }
private:
    int * ia;
};
    
int main()
{
    try{    
//<FocusPoint description = problem souce >
        testB pBs[1]={testD()};
//</FocusPoint>
    }catch(int){}       
    return 1;
}
</CPPCODE>
<Tests>
<Test1  environment="Linux G++2.96">
<ProgramResults>
    <result1>
        the result when commenting the "throw" statement 
        in the catch block of testD constructor:

        //temporary testD object constructor and destructor
        call testB(int=0):0xbffff980
        call testD()
        call ~testD:0xbffff980
        call ~testB:0xbffff980
        //sliced and bitwise copied testB object destructor
        call ~testB:0xbffff9a0
    </result1>

    <result2 myThoughts="I think when a exception accurs in the testD
constructor,
    then the testD's destructor should not be called!But the result
shows that not
    only the testD's destructor has been called but also the testB's
destructor has
    been called two times (actually testB's destructor only should be
called once">
    
        the result when uncommenting the "throw" statement
        in the catch block of testD constructor:

        //the result confuse me
        call testB(int=0):0xbffff980
        call ~testB:0xbffff980
        call ~testD:0xbffff980
        call ~testB:0xbffff980
    </result2>
</ProgramResults>
</Test1>
<Test2 environment="Window2000 VC++6.0">
<ProgramResults>
    <result1 myThought="the result shows only the testB's destructor
has been called
    once when the exception accurs and I think it's right">
        the result when uncommenting the "throw" statement
        in the catch block of testD constructor:

        //the result satisfy me!
        //the result's addresses is pseudo 
        call testB(int=0):0xbffff980
        call ~testB:0xbffff980  
    <result1>
</ProgramRestlts>
</Tests>
</Problem>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]