Topic: thrown object's dtor -- when called?
Author: hopps@mmm.com (Kevin J Hopps)
Date: 8 Feb 1995 18:06:07 GMT Raw View
When an object is thrown as an exception, when is its destructor
called?
In particular, look at the following code:
void f()
{
throw X();
}
int main()
{
X x0;
try {
f();
} catch(X x1) {
X x2;
}
return 0;
}
I wrote a simple X with a default ctor, a copy ctor and a
dtor, each of which print the address "this." What I found
was that the dtor for x1 is never called.
--
Kevin J. Hopps e-mail: kjhopps@mmm.com
3M Company phone: (612) 737-3300
3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 10 Feb 1995 00:12:26 GMT Raw View
In article dc@dawn.mmm.com, hopps@mmm.com (Kevin J Hopps) writes:
>When an object is thrown as an exception, when is its destructor
>called?
>
>In particular, look at the following code:
> void f()
> {
> throw X(); // 1
> }
>
> int main()
> {
> X x0;
> try {
> f();
> } catch(X x1) { // 2
> X x2;
> } // 3
> return 0;
> }
At #1, an X object is thrown, caught by value at #2. The original X
object should be destroyed after being copied into x1. x1 is local
to its catch-block, and should be destroyed at the end, namely at #3.
>I wrote a simple X with a default ctor, a copy ctor and a
>dtor, each of which print the address "this." What I found
>was that the dtor for x1 is never called.
That sounds like a compiler bug. Not too surprising, as this stuff is
hard to get right, and it's early days for exceptions.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: tony@online.tmx.com.au (Tony Cook)
Date: Sat, 11 Feb 1995 01:05:34 GMT Raw View
Steve Clamage (clamage@Eng.Sun.COM) wrote:
: In article dc@dawn.mmm.com, hopps@mmm.com (Kevin J Hopps) writes:
: >When an object is thrown as an exception, when is its destructor
: >called?
: >
: >In particular, look at the following code:
: > void f()
: > {
: > throw X(); // 1
: > }
: >
: > int main()
: > {
: > X x0;
: > try {
: > f();
: > } catch(X x1) { // 2
: > X x2;
: > } // 3
: > return 0;
: > }
: At #1, an X object is thrown, caught by value at #2. The original X
: object should be destroyed after being copied into x1. x1 is local
: to its catch-block, and should be destroyed at the end, namely at #3.
Shouldn't the original object be destroyed at 3 as well - since a rethrow
(using 'throw;' throws the original object, not the copy?
See the comments at the end of section 15.2 in the ARM too.
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sat, 11 Feb 1995 03:58:49 GMT Raw View
clamage@Eng.Sun.COM (Steve Clamage) writes:
>hopps@mmm.com (Kevin J Hopps) writes:
>>When an object is thrown as an exception, when is its destructor
>>called?
>>
>>In particular, look at the following code:
>> void f()
>> {
>> throw X(); // 1
>> }
>>
>> int main()
>> {
>> X x0;
>> try {
>> f();
>> } catch(X x1) { // 2
>> X x2;
>> } // 3
>> return 0;
>> }
>
>At #1, an X object is thrown, caught by value at #2.
Yes. That could either be
create temp
copy temp to thrown_X
destroy temp
copy thrown_X to x1
or it could be
create thrown_X
copy thrown_X to x1
since the compiler is allowed to elide temporaries in this case.
>The original X
>object should be destroyed after being copied into x1.
I don't think that is correct. Firstly, the original X might be a
short-lived temporary as outlined above. Secondly, what if the catch
body contains `throw;', or the catch parameter is a reference? The
thrown_X object should not be destroyed until the catch body is
completed.
>x1 is local
>to its catch-block, and should be destroyed at the end, namely at #3.
Yep.
>>I wrote a simple X with a default ctor, a copy ctor and a
>>dtor, each of which print the address "this." What I found
>>was that the dtor for x1 is never called.
>
>That sounds like a compiler bug. Not too surprising, as this stuff is
>hard to get right, and it's early days for exceptions.
All too true.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
all [L] (programming_language(L), L \= "Mercury") => better("Mercury", L) ;-)
Author: jason@cygnus.com (Jason Merrill)
Date: Sat, 11 Feb 1995 10:48:55 GMT Raw View
>>>>> Tony Cook <tony@online.tmx.com.au> writes:
> : At #1, an X object is thrown, caught by value at #2. The original X
> : object should be destroyed after being copied into x1. x1 is local
> : to its catch-block, and should be destroyed at the end, namely at #3.
> Shouldn't the original object be destroyed at 3 as well - since a rethrow
> (using 'throw;' throws the original object, not the copy?
A rethrow using 'throw;' rethrows the original *copy*, not the original
object.
Jason