* Strange thing about exceptions and destructors
@ 1997-05-12 4:00 Aleksandar Milivojevic
1997-05-12 4:00 ` Partha Sinha
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: Aleksandar Milivojevic @ 1997-05-12 4:00 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1742 bytes --]
I would like to know when the destructor for thrown object is called. I
expected that it is called only one time (and not two or three times)
after the catch block is executed. So in order to check that, I wrote
a simple program and it gave me three different outputs when compiled
with Sun CC, DEC cxx and GNU g++ (last gave me internal compiler
error, but when I commented last line of main() it compiled OK).
Now, Am I doing something totaly wrong, or that three compilers
generate incorect code (and which one generated correct code, if any)?
Here is program, and the outputs it gives when compiled with before
mentioned compilers:
#include <iostream.h>
class exp {
public:
exp(int x);
~exp();
int i;
};
exp::exp(int x) {
cout << "Constructor exp(" << x << ")\n";
i = x;
}
exp::~exp() {
cout << "Destructor ~exp()\n";
}
main() {
try {
cout << "Throwing exception\n";
throw exp(1);
} catch (exp a) {
cout << "Catched exp(" << a.i << ")\n";
}
cout << "Exiting\n";
}
When compiled with Sun CC:
Throwing exception
Constructor exp(1)
Catched exp(1)
Destructor ~exp()
Destructor ~exp()
Exiting
When compiled with DEC cxx:
Throwing exception
Constructor exp(1)
Destructor ~exp()
Catched exp(1)
Destructor ~exp()
Destructor ~exp()
Exiting
When compiled with GNU g++ (last line of main() commented):
Throwing exception
Constructor exp(1)
Destructor ~exp()
Catched exp(1)
--
Aleksandar Milivojevi� http://jagor.srce.hr/~alex/
e-mail: alex@srce.hr
================================ooooO=Ooooo==============================
Real Users never know what they want, but they always know when your
program doesn't deliver it.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Strange thing about exceptions and destructors
1997-05-12 4:00 Strange thing about exceptions and destructors Aleksandar Milivojevic
1997-05-12 4:00 ` Partha Sinha
@ 1997-05-12 4:00 ` Doug Harrison
1997-05-12 4:00 ` Allen Hadden
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Doug Harrison @ 1997-05-12 4:00 UTC (permalink / raw)
On 12 May 1997 20:39:57 +0200, Aleksandar Milivojevic <alex@srce.hr>
wrote:
>I would like to know when the destructor for thrown object is called. I
>expected that it is called only one time (and not two or three times)
>after the catch block is executed. So in order to check that, I wrote
>a simple program and it gave me three different outputs when compiled
>with Sun CC, DEC cxx and GNU g++ (last gave me internal compiler
>error, but when I commented last line of main() it compiled OK).
>
>Now, Am I doing something totaly wrong, or that three compilers
>generate incorect code (and which one generated correct code, if any)?
<snip>
Some suggestions: Add a copy-ctor that announces its invocation.
Besides announcing their invocation, have your ctors and dtor print
the object addresses.
--
Doug Harrison
dHarrison@worldnet.att.net
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Strange thing about exceptions and destructors
1997-05-12 4:00 Strange thing about exceptions and destructors Aleksandar Milivojevic
@ 1997-05-12 4:00 ` Partha Sinha
1997-05-12 4:00 ` Doug Harrison
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Partha Sinha @ 1997-05-12 4:00 UTC (permalink / raw)
To: Aleksandar Milivojevic
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 4050 bytes --]
Aleksandar Milivojevic wrote:
>
> I would like to know when the destructor for thrown object is called. I
> expected that it is called only one time (and not two or three times)
> after the catch block is executed.
Yes, you are absolutely right.
I tested the same program with Sun C++ 4.2 and gcc 2.7.2 .
Sun C++ does exactly what the standard says. The reason you are getting
two destructor messages is there are REALLY two constructions here.
One for the thrown object through the supplied constructor,
the other for the parameter "a" that is created in the catch() block
through default copy constructor. If you supply your own copy
constructor (and you should, if you have pointers in your class),
you can see the construction of "a". You should optimise here by
taking the reference to the thrown object since the reference to
the thrown object is valid when the call stack is unwound till we
reach a catch block. Change the
catch(exp a) to
catche(exp& a) // a is reference to the thrown object
and you will see one construction and one destruction.
Things are really strange with gcc 2.7.2 Apparently,
"throw exp(1);" creates one instance of exp through the
supplied constructor and then creates a copy of it, say x,
through copy constructor. And the catch parameter "a"
is constructed, as expected , from x with the copy constructor.
If we change the parameter to catch as a reference as
catch(exp& a), x is in any case constructed. The supplied
destrucor is called for neither a nor x.
I have the following code which results in memory leak
which means destructor for either a or x is not invoked.
class exp {
public:
exp(char *s) {
i = new char [10]; // allocate 10 instead of strlen(s)
strcpy(i,s);
cout << "Constructor exp(" << s << ")" << endl;
}
exp(const exp& a) { // copy constructor
i = new char [10]; // allocation
strcpy(i, a.i);
cout << "Copy Constructor exp from " << a.i << endl;
}
~exp() {
cout << "Destructor exp(" << i << ")" << endl;
delete [] i; // de-allocation
}
char *i;
};
int main() {
try {
cout << "Throwing exceptions" << endl;
throw exp("hello");
}
catch(exp a) {
cout << "Caught exception" << a.i << endl;
}
cout << "Exiting" << endl;
return 0;
}
I ran it through purify and it says 10 bytes leaked
out of 30 bytes (10* 3 (= # of objects exp("hello"), x and a)
allocated. Can any gcc guru explain this?
I don't have access to DEC but I am skeptic about exception
implementation in general. Both Sun C++ and gcc behaves same
without any leak, if you change the try-catch block to
following.
try {
...
throw new exp(1);
...
}
catch (exp* a) {
...
// even if you don't delete a, memory gets de-allocated, how?
}
Partha
psinha@ficon.compunnel.com
>
> #include <iostream.h>
>
> class exp {
> public:
> exp(int x);
> ~exp();
> int i;
> };
>
> exp::exp(int x) {
> cout << "Constructor exp(" << x << ")\n";
> i = x;
> }
>
> exp::~exp() {
> cout << "Destructor ~exp()\n";
> }
>
> main() {
> try {
> cout << "Throwing exception\n";
> throw exp(1);
> } catch (exp a) {
> cout << "Catched exp(" << a.i << ")\n";
> }
> cout << "Exiting\n";
> }
>
> When compiled with Sun CC:
> Throwing exception
> Constructor exp(1)
> Catched exp(1)
> Destructor ~exp()
> Destructor ~exp()
> Exiting
>
> When compiled with DEC cxx:
> Throwing exception
> Constructor exp(1)
> Destructor ~exp()
> Catched exp(1)
> Destructor ~exp()
> Destructor ~exp()
> Exiting
>
> When compiled with GNU g++ (last line of main() commented):
> Throwing exception
> Constructor exp(1)
> Destructor ~exp()
> Catched exp(1)
>
> --
> Aleksandar Milivojevi� http://jagor.srce.hr/~alex/
> e-mail: alex@srce.hr
> ================================ooooO=Ooooo==============================
>
> Real Users never know what they want, but they always know when your
> program doesn't deliver it.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Strange thing about exceptions and destructors
1997-05-12 4:00 Strange thing about exceptions and destructors Aleksandar Milivojevic
` (2 preceding siblings ...)
1997-05-12 4:00 ` Allen Hadden
@ 1997-05-12 4:00 ` Gene Thompson
1997-05-13 4:00 ` Philip Staite
1997-05-14 4:00 ` Aleksandar Milivojevic
5 siblings, 0 replies; 7+ messages in thread
From: Gene Thompson @ 1997-05-12 4:00 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3140 bytes --]
A,
There are quite a few things going on here.
1. You are catching you exception class by value. This makes 2 copys of the
object thrown.
2. You do not see this as you did not override the copy ctor as well.
3. g++ doesn't implement exception handling well, so don't use it for them.
4. Because of #1, the most correct is the DEC solution.
When you pass an exception object by value, the object is copied to a
temporary because the object you explicitly created goes out of scope when
the throw is executed. Then when you catch the temporary, it must be copied
into your local variable (in this case, 'a'). Thus you get 3 constructors
(2 copy and one explicit) and 3 destructors. You should change your catch
to use 'a' by reference. This way it is only copied once (you cannot get
around the throw copy).
Just as an aside, use "\n" in your cout calls does NOT flush the output
stream so you may not always get what you expect. You should use the endl
manipulator :
cout << "Hello World" << endl; // Executes a new line and flushes the
stream.
Good Luck,
Gene Thompson
PS. For a good discussion on exceptions, see "More Effective C++" by Scott
Meyers.
Aleksandar Milivojevic <alex@srce.hr> wrote in article
<gl7mh4mt4y.fsf@bjesomar.srce.hr>...
> I would like to know when the destructor for thrown object is called. I
> expected that it is called only one time (and not two or three times)
> after the catch block is executed. So in order to check that, I wrote
> a simple program and it gave me three different outputs when compiled
> with Sun CC, DEC cxx and GNU g++ (last gave me internal compiler
> error, but when I commented last line of main() it compiled OK).
>
> Now, Am I doing something totaly wrong, or that three compilers
> generate incorect code (and which one generated correct code, if any)?
>
> Here is program, and the outputs it gives when compiled with before
> mentioned compilers:
>
> #include <iostream.h>
>
> class exp {
> public:
> exp(int x);
> ~exp();
> int i;
> };
>
> exp::exp(int x) {
> cout << "Constructor exp(" << x << ")\n";
> i = x;
> }
>
> exp::~exp() {
> cout << "Destructor ~exp()\n";
> }
>
> main() {
> try {
> cout << "Throwing exception\n";
> throw exp(1);
> } catch (exp a) {
> cout << "Catched exp(" << a.i << ")\n";
> }
> cout << "Exiting\n";
> }
>
> When compiled with Sun CC:
> Throwing exception
> Constructor exp(1)
> Catched exp(1)
> Destructor ~exp()
> Destructor ~exp()
> Exiting
>
> When compiled with DEC cxx:
> Throwing exception
> Constructor exp(1)
> Destructor ~exp()
> Catched exp(1)
> Destructor ~exp()
> Destructor ~exp()
> Exiting
>
> When compiled with GNU g++ (last line of main() commented):
> Throwing exception
> Constructor exp(1)
> Destructor ~exp()
> Catched exp(1)
>
> --
> Aleksandar Milivojevi� http://jagor.srce.hr/~alex/
> e-mail: alex@srce.hr
> ================================ooooO=Ooooo==============================
>
> Real Users never know what they want, but they always know when your
> program doesn't deliver it.
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Strange thing about exceptions and destructors
1997-05-12 4:00 Strange thing about exceptions and destructors Aleksandar Milivojevic
1997-05-12 4:00 ` Partha Sinha
1997-05-12 4:00 ` Doug Harrison
@ 1997-05-12 4:00 ` Allen Hadden
1997-05-12 4:00 ` Gene Thompson
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Allen Hadden @ 1997-05-12 4:00 UTC (permalink / raw)
To: Aleksandar Milivojevic
Aleksandar Milivojevic wrote:
>
> I would like to know when the destructor for thrown object is called. I
> expected that it is called only one time (and not two or three times)
> after the catch block is executed. So in order to check that, I wrote
> a simple program and it gave me three different outputs when compiled
> with Sun CC, DEC cxx and GNU g++ (last gave me internal compiler
> error, but when I commented last line of main() it compiled OK).
>
> Now, Am I doing something totaly wrong, or that three compilers
> generate incorect code (and which one generated correct code, if any)?
>
> Here is program, and the outputs it gives when compiled with before
> mentioned compilers:
>
> #include <iostream.h>
>
> class exp {
> public:
> exp(int x);
> ~exp();
> int i;
> };
>
> exp::exp(int x) {
> cout << "Constructor exp(" << x << ")\n";
> i = x;
> }
>
> exp::~exp() {
> cout << "Destructor ~exp()\n";
> }
>
> main() {
> try {
> cout << "Throwing exception\n";
> throw exp(1);
> } catch (exp a) {
> cout << "Catched exp(" << a.i << ")\n";
> }
> cout << "Exiting\n";
> }
>
Not sure why they are giving such different results. A copy of the
thrown exp is getting created. If you add a copy constructor for
exp and put some debug code there, you may be able to see what is
going on.
exp( const exp& copy )
{
// Perform the copy
i = copy.i;
cout << "Copy constructor" << endl;
}
Also, you should probably not catch by value. Try catching by
reference instead:
main() {
try {
cout << "Throwing exception\n";
throw exp(1);
} catch (exp& a) {
cout << "Catched exp(" << a.i << ")\n";
}
cout << "Exiting\n";
}
-Allen
--
Allen Hadden
Systems Engineer/Laboratory Application Specialist
Taratec Development Corporation
ahadden@taratec.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Strange thing about exceptions and destructors
1997-05-12 4:00 Strange thing about exceptions and destructors Aleksandar Milivojevic
` (3 preceding siblings ...)
1997-05-12 4:00 ` Gene Thompson
@ 1997-05-13 4:00 ` Philip Staite
1997-05-14 4:00 ` Aleksandar Milivojevic
5 siblings, 0 replies; 7+ messages in thread
From: Philip Staite @ 1997-05-13 4:00 UTC (permalink / raw)
Aleksandar Milivojevic wrote:
>
> I would like to know when the destructor for thrown object is called. I
> expected that it is called only one time (and not two or three times)
> after the catch block is executed. So in order to check that, I wrote
> a simple program and it gave me three different outputs when compiled
> with Sun CC, DEC cxx and GNU g++ (last gave me internal compiler
> error, but when I commented last line of main() it compiled OK).
>
> Now, Am I doing something totaly wrong, or that three compilers
> generate incorect code (and which one generated correct code, if any)?
Try this version with "noisy" copy constructor and assignment operator.
#include <iostream.h>
class exp {
public:
exp( int x ) : i( x ) {
cout << "ctor -- " << *this << endl; }
~exp() {
cout << "dtor -- " << *this << endl; }
exp( const exp& e ) : i( e.i ) {
cout << "copy ctor -- " << *this << " from " << e << endl; }
exp& operator=( const exp& e ) {
cout << "assignment " << *this << " = " << e << endl;
if( &e != this )
i = e.i;
return *this; }
friend ostream& operator<<( ostream& o, const exp& e ) {
return o << "exp{" << e.i << "} at: " << &e; }
private:
int i;
};
main() {
try {
cout << "Throwing exception\n";
throw exp(1);
} catch (exp a) {
cout << "Caught " << a << endl;
}
cout << "Exiting\n";
}
When compiled with xlC on AIX I get:
~> a.out
Throwing exception
ctor -- exp{1} at: 0x2ff220f0
copy ctor -- exp{1} at: 0x2ff220f8 from exp{1} at: 0x2ff220f0
Caught exp{1} at: 0x2ff220f8
dtor -- exp{1} at: 0x2ff220f8
dtor -- exp{1} at: 0x2ff220f0
Exiting
~>
--
Phil Staite If I were an IBM spokesperson I'd have
pstaite @ vnet.ibm.com business cards, not disclaimers.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Strange thing about exceptions and destructors
1997-05-12 4:00 Strange thing about exceptions and destructors Aleksandar Milivojevic
` (4 preceding siblings ...)
1997-05-13 4:00 ` Philip Staite
@ 1997-05-14 4:00 ` Aleksandar Milivojevic
5 siblings, 0 replies; 7+ messages in thread
From: Aleksandar Milivojevic @ 1997-05-14 4:00 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 415 bytes --]
Thanks to everybody who replied and explained that copy of thrown
object was created.
--
Aleksandar Milivojevi� http://jagor.srce.hr/~alex/
e-mail: alex@srce.hr
================================ooooO=Ooooo==============================
Real Users never know what they want, but they always know when your
program doesn't deliver it.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~1997-05-14 4:00 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-05-12 4:00 Strange thing about exceptions and destructors Aleksandar Milivojevic
1997-05-12 4:00 ` Partha Sinha
1997-05-12 4:00 ` Doug Harrison
1997-05-12 4:00 ` Allen Hadden
1997-05-12 4:00 ` Gene Thompson
1997-05-13 4:00 ` Philip Staite
1997-05-14 4:00 ` Aleksandar Milivojevic
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox