Bug#151065: g++: destructor called twice
Package: g++
Version: 2:2.95.4-14
Severity: normal
Hi !
We have problems with a C++ program reported in detail below.
It is not a Debian specific problem. In particular we checked:
- there is the same problem using GNU C++ compiler on SuSE Linux
and MacOS X
- Code compiles and executes properly on MacOS using the
CodeWarrior compiler
- Problem does not go away with g++ version 3.0
The code below compiles but when the expression
z = 3.0*x in the main function is executed the destructur of some
temporary object is called twice, leading to a segmentation
fault.
In addition there is a second problem: When the line
z = 3.0*x in main() is changed to z = x*3.0 the compiler
tries to instantiate the function template
T_leaftype operator* (double alpha, T_leaftype x) with
T_leaftype=double, but it should use the method operator*(double)
of class ScalarVector which is inherited from Vector<ScalarVector>
The code compiles and executes properly with the
CodeWarrior compiler on MacOS.
Thanks for considering this problem
------------------------>---<-----------------------------
#include<iostream>
#include<string>
using namespace std;
// The base class
template <class T_leaftype>
class Vector {
public:
T_leaftype operator* (double alpha);
protected:
Vector () {}
private:
T_leaftype& asLeaf ()
{ return static_cast<T_leaftype&>(*this);}
} ;
template <class T_leaftype>
inline T_leaftype Vector<T_leaftype>::operator* (double alpha)
{
T_leaftype z(asLeaf());
return z;
}
template <class T_leaftype>
inline T_leaftype operator* (double alpha, T_leaftype x)
{
return x.operator*(alpha);
}
// The derived class
class ScalarVector : public Vector<ScalarVector> {
public:
ScalarVector (int elements);
ScalarVector (const ScalarVector&);
~ScalarVector ();
private:
int n; // number of doubles
double *v; // array of values
} ;
ScalarVector::ScalarVector (int elements)
{
n = elements;
v = new double[n];
cout << "making ScalarVector v=" << v << endl;
}
ScalarVector::ScalarVector (const ScalarVector& x)
{
n = x.n;
v = new double[n];
for (int i=0; i<n; i++) v[i] = x.v[i];
cout << "making ScalarVector v=" << v << endl;
}
ScalarVector::~ScalarVector ()
{
cout << "delete ScalarVector v=" << v << endl;
delete[] v;
}
// main program
int main ()
{
ScalarVector x(3), z(3);
z = 3.0*x;
}
-- System Information
Debian Release: 3.0
Architecture: i386
Kernel: Linux luft 2.4.18 #1 Wed Jun 12 14:53:15 CEST 2002 i686
Locale: LANG=de_DE, LC_CTYPE=de_DE
Versions of packages g++ depends on:
ii cpp 2:2.95.4-14 The GNU C preprocessor.
ii g++-2.95 1:2.95.4-7 The GNU C++ compiler.
ii gcc-2.95 1:2.95.4-7 The GNU C compiler.
--
To UNSUBSCRIBE, email to debian-gcc-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Reply to: