Bug 48282

Summary: destructor called one too many times?
Product: [Retired] Red Hat Linux Reporter: eeeg
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED CURRENTRELEASE QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 7.1   
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2010-06-14 14:18:25 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

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:
Always

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

using namespace std;

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

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

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

};

void foo(a an_a) {}

int main(void) {
   a my_a;
   foo(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)