[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#151065: marked as done (g++: destructor called twice)



Your message dated Fri, 12 Jul 2002 09:37:52 +0200
with message-id <15662.34640.753151.411760@gargle.gargle.HOWL>
and subject line Not a bug: Re: Bug#151065: g++: destructor called twice
has caused the attached Bug report to be marked as done.

This means that you claim that the problem has been dealt with.
If this is not the case it is now your responsibility to reopen the
Bug report if necessary, and/or fix the problem forthwith.

(NB: If you are a system administrator and have no idea what I am
talking about this indicates a serious mail system misconfiguration
somewhere.  Please contact me immediately.)

Debian bug tracking system administrator
(administrator, Debian Bugs database)

--------------------------------------
Received: (at submit) by bugs.debian.org; 26 Jun 2002 13:06:47 +0000
>From peter@luft.iwr.uni-heidelberg.de Wed Jun 26 08:06:47 2002
Return-path: <peter@luft.iwr.uni-heidelberg.de>
Received: from mail.iwr.uni-heidelberg.de [129.206.104.30] 
	by master.debian.org with esmtp (Exim 3.12 1 (Debian))
	id 17NCVT-0003Tb-00; Wed, 26 Jun 2002 08:06:47 -0500
Received: from hal.iwr.uni-heidelberg.de (IDENT:+vDqd+vIUMta8OiYlKHVgBEV4k3SEdoa@hal.iwr.uni-heidelberg.de [129.206.69.248])
	by mail.iwr.uni-heidelberg.de (8.11.1/8.11.1) with ESMTP id g5QD6ip12799
	for <submit@bugs.debian.org>; Wed, 26 Jun 2002 15:06:44 +0200 (MET DST)
Received: from luft.iwr.uni-heidelberg.de ([129.206.69.130])
	by hal.iwr.uni-heidelberg.de with esmtp (Exim 3.35 #1 (Debian))
	id 17NCVP-0006Gf-00
	for <submit@bugs.debian.org>; Wed, 26 Jun 2002 15:06:43 +0200
Received: from peter by luft.iwr.uni-heidelberg.de with local (Exim 3.35 #1 (Debian))
	id 17NCVC-00020q-00; Wed, 26 Jun 2002 15:06:30 +0200
From: Peter Bastian <peter@luft.iwr.uni-heidelberg.de>
To: Debian Bug Tracking System <submit@bugs.debian.org>
Subject: g++: destructor called twice
X-Mailer: reportbug 1.50
Date: Wed, 26 Jun 2002 15:06:30 +0200
Message-Id: <E17NCVC-00020q-00@luft.iwr.uni-heidelberg.de>
Delivered-To: submit@bugs.debian.org

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.


---------------------------------------
Received: (at 151065-done) by bugs.debian.org; 12 Jul 2002 10:05:12 +0000
>From doko@cs.tu-berlin.de Fri Jul 12 05:05:12 2002
Return-path: <doko@cs.tu-berlin.de>
Received: from mail.cs.tu-berlin.de [130.149.17.13] (root)
	by master.debian.org with esmtp (Exim 3.12 1 (Debian))
	id 17SxIV-00011z-00; Fri, 12 Jul 2002 05:05:12 -0500
Received: from bolero.cs.tu-berlin.de (root@bolero.cs.tu-berlin.de [130.149.19.1])
	by mail.cs.tu-berlin.de (8.9.3/8.9.3) with ESMTP id LAA22226;
	Fri, 12 Jul 2002 11:58:13 +0200 (MET DST)
Received: (from doko@localhost)
	by bolero.cs.tu-berlin.de (8.11.6+Sun/8.9.3) id g6C7bq729371;
	Fri, 12 Jul 2002 09:37:52 +0200 (MEST)
From: Matthias Klose <doko@cs.tu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-ID: <15662.34640.753151.411760@gargle.gargle.HOWL>
Date: Fri, 12 Jul 2002 09:37:52 +0200
To: Philip Martin <philip@codematters.co.uk>, 151065-done@bugs.debian.org
Cc: Peter Bastian <peter@luft.iwr.uni-heidelberg.de>
Subject: Not a bug: Re: Bug#151065: g++: destructor called twice
In-Reply-To: <87k7oj3s0o.fsf@codematters.co.uk>
References: <87k7oj3s0o.fsf@codematters.co.uk>
X-Mailer: VM 7.03 under 21.4 (patch 6) "Common Lisp" XEmacs Lucid
Delivered-To: 151065-done@bugs.debian.org

therefore closing the report.

Philip Martin writes:
> The segmentation fault is a bug in your code, not in gcc.  A temporary
> is getting constructed, and you are using the compiler generated
> assignment operator, so you get two instances deallocating the same
> memory. Try adding a private operator=
> 
>   class ScalarVector : public Vector<ScalarVector> {
>   private:
>      ScalarVector& operator=(const ScalarVector&);
>   };
> 
> and your code will fail to compile.  Provide a suitable public
> operator= and your code should work.
> 
> 
> I believe the other problem can be simplified to
> 
> struct A {
>    double operator*(double);
> };
> 
> template<typename T> T operator*(double, T);
> 
> int main() {
>    A a;
>    a * 3.0;
> }
> 
> which gives the error
> z2.cc: In function `int main()':
> z2.cc:5: `T operator*(double, const T&) [with T = double]' must have an 
>    argument of class or enumerated type
> 
> -- 
> Philip
> 
> 
> -- 
> To UNSUBSCRIBE, email to debian-gcc-request@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org


-- 
To UNSUBSCRIBE, email to debian-gcc-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org



Reply to: