?
=?ISO-8859-1?Q?Fran=E7ois_Duranleau?=
Hi!
I was writing some piece of code and then, after pondering on some
readings in "More Effective C++" by Scott Meyers (Item 10) and the book of
Stroustrup (section 14.4.1 in the hardcover special edition), I tested one
of their solutions to write constructors that avoid memory leaks upon
exceptions. But the leaks were still there!
So I wrote a simple test case and here is the code:
%%%%%
#include <iostream>
using namespace std ;
class chose_binouche
{
public :
chose_binouche()
{
cout << "ctor" << endl ;
}
~chose_binouche()
{
cout << "dtor" << endl ;
}
} ;
class bidon
{
public :
chose_binouche a ;
chose_binouche b ;
bidon()
: a() ,
b()
{
throw "exception" ;
}
~bidon()
{
}
} ;
int
main()
{
bidon b ;
return 0 ;
}
%%%%%
In theory (according to the readings mentionned above), the destructors
for the field a and b in class bidon should be called after the exception
is thrown in the constructor; however, the output of the program was:
ctor
ctor
Aborted (core dumped)
No dtor displayed!
The compiler I am using is g++-3.3.3 and g++-3.4.0 on a Fedora Core 2
Linux station, and I was about to send a bug report but then again maybe
there is something I didn't get right. Unfortunately, I do not have
another compiler at hand to see what it does on other environments.
Can someone else confirm this or prove me wrong?
I was writing some piece of code and then, after pondering on some
readings in "More Effective C++" by Scott Meyers (Item 10) and the book of
Stroustrup (section 14.4.1 in the hardcover special edition), I tested one
of their solutions to write constructors that avoid memory leaks upon
exceptions. But the leaks were still there!
So I wrote a simple test case and here is the code:
%%%%%
#include <iostream>
using namespace std ;
class chose_binouche
{
public :
chose_binouche()
{
cout << "ctor" << endl ;
}
~chose_binouche()
{
cout << "dtor" << endl ;
}
} ;
class bidon
{
public :
chose_binouche a ;
chose_binouche b ;
bidon()
: a() ,
b()
{
throw "exception" ;
}
~bidon()
{
}
} ;
int
main()
{
bidon b ;
return 0 ;
}
%%%%%
In theory (according to the readings mentionned above), the destructors
for the field a and b in class bidon should be called after the exception
is thrown in the constructor; however, the output of the program was:
ctor
ctor
Aborted (core dumped)
No dtor displayed!
The compiler I am using is g++-3.3.3 and g++-3.4.0 on a Fedora Core 2
Linux station, and I was about to send a bug report but then again maybe
there is something I didn't get right. Unfortunately, I do not have
another compiler at hand to see what it does on other environments.
Can someone else confirm this or prove me wrong?