 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
kib Guest
|
Posted: Thu Apr 29, 2004 10:00 pm Post subject: delete[] and exceptions in array element destructors |
|
|
The standard, 15.2, paragraph 2 says
An object that is partially constructed or partially destroyed will
have destructors executed for all of its fully constructed subobjects,
that is, for subobjects for which the constructor has completed
execution and the destructor has not yet begun execution.
I read this as requirement for delete[] x to call the destructors of
all elements x, even if some destructor of x[i] raises exception in
the course of action. Is this true ?
The following test program
#include <iostream>
class A
{
public:
static int c;
A();
~A();
void f();
};
int A::c = 0;
A::A()
{
c++;
std::cerr << "Constructing " << c << std::endl;
}
A::~A()
{
std::cerr << "Destructing " << c << std::endl;
c--;
if (c == 5)
throw 5;
}
void
A::f()
{
std::cerr << "A::f()" << std::endl;
}
int
main()
{
try
{
A a[10];
a[10].f();
}
catch (int& i)
{
std::cerr << "Caught " << i << std::endl;
}
return 0;
}
was run under g++ 3.4.0, Sun C++ 5.5 2003/03/12 and
MS VC++ (.NET 2003) 13.10.3077.
For gcc and CC, I got
Constructing 1
Constructing 2
Constructing 3
Constructing 4
Constructing 5
Constructing 6
Constructing 7
Constructing 8
Constructing 9
Constructing 10
A::f()
Destructing 10
Destructing 9
Destructing 8
Destructing 7
Destructing 6
Caught 5
For VC++,
Constructing 1
Constructing 2
Constructing 3
Constructing 4
Constructing 5
Constructing 6
Constructing 7
Constructing 8
Constructing 9
Constructing 10
A::f()
Destructing 10
Destructing 9
Destructing 8
Destructing 7
Destructing 6
Destructing 5
Destructing 4
Destructing 3
Destructing 2
Destructing 1
Caught 5
If my claim true, then gcc and CC do not conform to this standard
clause, and VC++ conforms, isn't it ?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Paul D. DeRocco Guest
|
Posted: Fri Apr 30, 2004 11:26 am Post subject: Re: delete[] and exceptions in array element destructors |
|
|
| Quote: | "kib" <kostikbel (AT) ukr (DOT) net> wrote
The standard, 15.2, paragraph 2 says
An object that is partially constructed or partially destroyed will
have destructors executed for all of its fully constructed subobjects,
that is, for subobjects for which the constructor has completed
execution and the destructor has not yet begun execution.
I read this as requirement for delete[] x to call the destructors of
all elements x, even if some destructor of x[i] raises exception in
the course of action. Is this true ?
|
That's a bit too lawyerly for me. However, if you allow your destructors to
throw exceptions, then I have no sympathy for your plight. ;-)
--
Ciao, Paul D. DeRocco
Paul mailto:pderocco (AT) ix (DOT) netcom.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Sat May 01, 2004 2:58 am Post subject: Re: delete[] and exceptions in array element destructors |
|
|
[email]kostikbel (AT) ukr (DOT) net[/email] (kib) wrote in message
news:<1629a38d.0404290257.2c521d43 (AT) posting (DOT) google.com>...
| Quote: | The standard, 15.2, paragraph 2 says
An object that is partially constructed or partially destroyed will
have destructors executed for all of its fully constructed subobjects,
that is, for subobjects for which the constructor has completed
execution and the destructor has not yet begun execution.
I read this as requirement for delete[] x to call the destructors of
all elements x, even if some destructor of x[i] raises exception in
the course of action. Is this true ?
|
Yes.
Of course, if two or more of the destructors raises an exception, the
implementation should call terminate.
| Quote: | If my claim true, then gcc and CC do not conform to this standard
clause, and VC++ conforms, isn't it ?
|
That's what it looks like.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vladimír Marko Guest
|
Posted: Sat May 01, 2004 12:07 pm Post subject: Re: delete[] and exceptions in array element destructors |
|
|
"kib" <kostikbel (AT) ukr (DOT) net> wrote
| Quote: | The standard, 15.2, paragraph 2 says
An object that is partially constructed or partially destroyed will
have destructors executed for all of its fully constructed subobjects,
that is, for subobjects for which the constructor has completed
execution and the destructor has not yet begun execution.
I read this as requirement for delete[] x to call the destructors of
all elements x, even if some destructor of x[i] raises exception in
the course of action. Is this true ?
The following test program
[snip]
If my claim true, then gcc and CC do not conform to this standard
clause, and VC++ conforms, isn't it ?
|
I guess the intent is to have all created objects destroyed,
but I found nothing that would describe that specific
case in standard. The next sentence in the paragraph you
quoted says
"Should a constructor for an element of an automatic
array throw an exception, only the constructed
elements of that array will be destroyed."
and something similar should be applied to throwing
in the destructor, but it is NOT present in 15.2.
The fact that the standard explicitly mentions throw
from array element constructor and fails to mention
throw from array element destructor could be seen
as a defect. Try to post it to comp.std.c++.
However, when a dtor of an array element throws,
we have a lose-lose situation. g++ chooses not to
call the dtors for the remaining elements which will
often result into some resource leaks. On the other
hand, VC++ tries to call the dtors and runs a very
real risk of calling std::terminate() if another dtor
throws.
While I believe that the second behaviour is correct,
this should be only an academic issue. Programmers
should follow the well meant note in 15.2/3:
"So destructors should generally catch exceptions
and not let them propagate out of the destructor."
Vladimir Marko
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|