By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

array exception safety

P: n/a
if arrays were exception save (as i thought) the following should give
2 destructors for the partly constructed x2. unfortunately none of the
compilers i tried (g++3.3.1, vc++.net, bcc32, icc) conforms to this.

when using the (commented) Xs-ctor with try-rethrow at least g++ and
vc++.net show really two destructors.

even worse, when taking the vector-based Xsv instead if Xs in main
there is only ONE dtor-call and two with the try-rethrowing Xsv-ctor.

very puzzled:
k ahrens

//-------------------------------------------------------------------
#include <iostream>
#include <vector>

using std::cerr;
using std::endl;
using std::vector;

class X {
static int c;
public:
X(){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
X(const X&){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
~X() { ++c; cerr<<"~X()"<<endl;;}
};

int X::c=2;

class Xs {
int n;
X* data;
public:
Xs(int i):n(i), data(new X[i]){}
// Xs(int i) try :n(i), data(new X[i]){}catch(...){throw;}
~Xs(){ delete[] data; }
};

class Xsv {
int n;
vector<X> *data;
public:
Xsv(int i):n(i), data(new vector<X>(i)){}
// Xsv(int i) try :n(i), data(new vector<X>(i)){}
// catch(...){throw;}
~Xsv(){ delete data; }
};

int main()
{
{Xs x2(4);} // resp. Xsv...
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
#include <iostream>
#include <vector>

using std::cerr;
using std::endl;
using std::vector;

class X {
static int c;
public:
X(){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
X(const X&){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
~X() { ++c; cerr<<"~X()"<<endl;;}
};

int X::c=2;

class Xs {
int n;
X* data;
public:
// Xs(int i):n(i), data(new X[i]){}
Xs(int i) try :n(i), data(new X[i]){}catch(...){throw;}
~Xs(){ delete[] data; }
};

class Xsv {
int n;
vector<X> *data;
public:
// Xsv(int i):n(i), data(new vector<X>(i)){}
Xsv(int i) try :n(i), data(new vector<X>(i)){}
catch(...){throw;}
~Xsv(){ delete data; }
};

int main()
{
{Xsv x2(4);} // resp. Xsv...
}
//-------------------------------------------------------------------
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mon, 08 Dec 2003 13:20:47 +0100, Klaus Ahrens
<ah****@informatik.hu-berlin.de> wrote:
if arrays were exception save (as i thought) the following should give
2 destructors for the partly constructed x2. unfortunately none of the
compilers i tried (g++3.3.1, vc++.net, bcc32, icc) conforms to this.
Arrays are fine for exception safety. AFAIK, those compilers have no
trouble with destroying partly constructed arrays.

when using the (commented) Xs-ctor with try-rethrow at least g++ and
vc++.net show really two destructors.

even worse, when taking the vector-based Xsv instead if Xs in main
there is only ONE dtor-call and two with the try-rethrowing Xsv-ctor.

very puzzled:


If you don't catch an exception (you let it escape from main),
std::terminate is called, and destructors may not be called (it is
implementation defined whether the stack unwinds or not before the
terminate call). You can fix you programs by putting a try/catch in
main, thus forcing a stack unwind.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.