Bug 9946 - [3.2 regression] destructor is not called for temporary object
Summary: [3.2 regression] destructor is not called for temporary object
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2003-03-04 20:26 UTC by vasilche
Modified: 2003-07-25 17:33 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description vasilche 2003-03-04 20:26:01 UTC
If expression of type 'const C' (not const reference) is passed to function accepting 'C' by value, gcc 3.2.0-3.2.2 doesn't call destructor for temporary argument object.
---- The code destr.cpp:
#include <iostream>

struct C
{
    C(void)       { std::cerr << this << "->C()\n"; }
    C(const C& c) { std::cerr << this << "->C(" << &c << ")\n"; }
    ~C(void)      { std::cerr << this << "->~C()\n"; }
};

struct C2
{
    const C get(void) const { return c; }
    void go(C c) {}
    C c;
};

int main()
{
    C2 c2;
    c2.go(c2.get());
}
---- Command line:
# g++ -O0 -o destr destr.cpp
---- Output:
~/tmp# ./destr
0xbffff604->C()
0xbffff5f4->C(0xbffff604)
0xbffff604->~C()
~/tmp#

Release:
gcc-3.2.2

Environment:
Linux linus 2.4.17-aa #6 SMP Thu Jan 3 17:53:55 EST 2002 i686 unknown

How-To-Repeat:
Returned object value should be const.
Argument should be object by value.
Compilation should be without optimization.
With optimization turned on all destructors are called correctly.
Comment 1 Wolfgang Bangerth 2003-03-04 22:41:50 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed, a regression in 3.2 w.r.t 3.0. It is fixed in
    3.3 and mainline, though.
    
    W.
Comment 2 Joe Buck 2003-04-25 19:50:13 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed for the next release (3.3).