Bug 48282 - destructor called one too many times?
Summary: destructor called one too many times?
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 7.1
Hardware: i686
OS: Linux
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
Depends On:
TreeView+ depends on / blocked
Reported: 2001-07-10 02:16 UTC by eeeg
Modified: 2010-06-14 14:18 UTC (History)
0 users

Clone Of:
Last Closed: 2010-06-14 14:18:25 UTC

Attachments (Terms of Use)

Description eeeg 2001-07-10 02:16:14 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.2) Gecko/20010701

Description of problem:
It seems (I'm know authority on the C++ standard) that the destructor is
called one too many times when an object is passed by value to a function.
 When the user supplies their own copy constructor the destructor is called
only twice (which is what I expect).  The same behavior is seen with
rh6.2,7.0,7.1 as well as gcc 3.0.  The program in 'Steps to Reproduce'
demonstrates this.

How reproducible:

Steps to Reproduce:
> cat destruct.cc 
#include <iostream>

using namespace std;

class a {
   a() {
      cout << "Made an a." << '\n';

   ~a() { 
      cout << "Destructor called." << '\n';

#ifdef MY_CP_CON 
   a(const a& rhs) {
      cout << "copy con called" << '\n';


void foo(a an_a) {}

int main(void) {
   a my_a;
   return 0;

> g++ -o destruct destruct.cc && ./destruct
Made an a.
Destructor called.
Destructor called.
Destructor called.

> g++ -o destruct -DMY_CP_CON destruct.cc && ./destruct
Made an a.
copy con called
Destructor called.
Destructor called.

Actual Results:  Given in steps to reproduce.

Expected Results:  I'd expect 'g++ -o destruct destruct.cc && ./destruct'
to produce:

Made an a.
Destructor called.
Destructor called.

Additional info:

Comment 1 Matthew Miller 2007-04-10 19:07:02 UTC
I'm just going through and checking on old bugs -- if you still have interest,
could you recheck against the latest Fedora Core release? Thanks.

Comment 2 Denys Vlasenko 2008-07-31 15:05:51 UTC
does not happen with:

# g++ -v
Using built-in specs.
Target: x86_64-redhat-linux
gcc version 4.3.0 20080428 (Red Hat 4.3.0-8) (GCC)

Note You need to log in before you can comment on or make changes to this bug.