Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > array exception safety

Reply
Thread Tools

array exception safety

 
 
Klaus Ahrens
Guest
Posts: n/a
 
      12-08-2003
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...
}
//-------------------------------------------------------------------


 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      12-08-2003
On Mon, 08 Dec 2003 13:20:47 +0100, Klaus Ahrens
<(E-Mail Removed)-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
STL exception safety, operator== Luke Meyers C++ 0 01-05-2006 10:17 PM
C-style File Operations, C++ Streams, Exception Safety Scott Brady Drummonds C++ 3 01-18-2005 06:15 PM
Exception safety in the STL John Harrison C++ 5 07-08-2003 05:55 AM



Advertisments