🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.hp.hpux
Article #19

Bug in CC exception handling?

#19
From: Michael Bresnahan
Date: Sat, 04 Nov 1995 21:29
140 lines
3546 bytes
This simple program illustrates what looks like a bug in the exception handling
stack unwinding code of HP's CC/libCC or there is something about C++ I don't
understand.  It shows the same object being destructed twice, and one object
left hanging.  I am using:

CC:	HP C++ HPCPLUSPLUS  A.03.70
libC:	HP C++ HPCPLUSPLUS  A.03.70
OS:	HP-UX devux01 A.09.04 B 9000/891 342319241 16-user license

I have one base object Dbbase and two derived objects from it, Dbctx and
Dbconn.  The program creates a Dbctx object and then calls the member function
connect().  This function is supposed to return a Dbconn object, however the
constructor for Dbconn throws an exception.  The stack is unrolled, but instead
of destructing the base object of the partially constructed dbconn object, the
base object of the Dbctx object is destructed.  Then the base object of Dbctx
is destructed again when the Dbctx object is destructed.  Is this making any
sense?  Hopefully the code and output will speak for itself.

Here is the code:

#include <iostream.h>
#include <stdlib.h>

class Dbconn;

class my_ex { };

class Dbbase {
protected:

    Dbbase();
    virtual ~Dbbase();

private:

    char* data;
};

class Dbctx : public Dbbase {
public:

    // Construction
    Dbctx();

    // Destruction
    virtual ~Dbctx();

    Dbconn connect();
};

class Dbconn : public Dbbase {
public:

    Dbconn();

    virtual ~Dbconn();

};

int main() {

    try {
        Dbctx ctx;
        Dbconn conn = ctx.connect();
    }
    catch( my_ex& ex) {
        cout << "Exception caught!" << endl;
        exit(1);
    }
}

Dbbase::Dbbase() {

    cout << "Dbbase(): " << this << endl;

    data = new char[8192];

    cout << "\tdata: " << (void*)data << endl;
}

Dbbase::~Dbbase() {

    cout << "~Dbase(): " << this <<  endl;
    cout << "\tdata: " << (void*)data << endl;

    delete data;
}

Dbctx::Dbctx() : Dbbase() {

    cout << "Dbctx(): " << this << endl;
}

Dbctx::~Dbctx() {

    cout << "~Dbctx(): " << this << endl;
}

Dbconn Dbctx::connect() {

    return Dbconn();
}

Dbconn::Dbconn() : Dbbase() {

    cout << "Dbconn(): " << this << endl;

    throw my_ex();
}

Dbconn::~Dbconn() {

    cout << "~Dbconn: " << this << endl;
}

Here is how I compiled it:

CC bug.cc +eh -o bug 

And here is the ouput:

Dbbase(): 0x7b03350c
        data: 0x40008000
Dbctx(): 0x7b03350c
Dbbase(): 0x7b033514
        data: 0x4000a008
Dbconn(): 0x7b033514
~Dbase(): 0x7b03350c              // This should be 0x7b033514
        data: 0x40008000          // This should be 0x4000a008
~Dbctx(): 0x7b03350c
~Dbase(): 0x7b03350c
        data: 0x40008000
Exception caught!

Note that the Dbctx object at 0x7b03350c gets freed twice and the Dbconn object
is left floating.  Am I missing something or is this a bug in HP's code?  This
is holding my project up.  I may have to forego the use of exceptions and use a
more traditional error handling mechanism.

MikeB
Technical Details
Message-ID: <47gm0m$j26@blackice.winternet.com>
Path: arc1-ca.pugleaf.net!winternet.com!news

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!winternet.com!news
From: Michael Bresnahan <mbresnahan1@mmm.com>
Newsgroups: comp.sys.hp.hpux,comp.lang.c++
Subject: Bug in CC exception handling?
Date: 4 Nov 1995 21:29:58 GMT
Organization: 3M Company
Message-ID: <47gm0m$j26@blackice.winternet.com>
NNTP-Posting-Host: gudu.winternet.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 1.1N (X11; I; Linux 1.3.20 i586)
X-URL: news:comp.sys.hp.hpux