public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/33799]  New: Return value's destructor not executed when a local variable's destructor throws
@ 2007-10-17 11:31 bitti at iki dot fi
  2007-10-17 12:03 ` [Bug c++/33799] " rguenth at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 24+ messages in thread
From: bitti at iki dot fi @ 2007-10-17 11:31 UTC (permalink / raw)
  To: gcc-bugs

In C++, if a local variable's destructor throws after the return value object
has been created, the return value object is never destroyed!
G++ uses the allowed C++ return value optimisation and creates a return value
object directly without copying it. This is probably one source of the bug.

This bug manifests in all versions of g++ (3.2.3 - 4.2.2) I happened to have on
my machine, it also shows on both i686 and x86_64 architectures. It seems that
throwing destructors are rare enough so that this bug has gone unnoticed for
really long... :)

Test case:
-------- etest.cc --------------
#include <iostream>
#include <ostream>
using namespace std;

class X
{
public:
  X(bool throws) : throws_(throws)
  {
    cerr << "X::X() (" << throws_ << ")" << endl;
  }

  X(const X& x) : throws_(x.throws_)
  {
    cerr << "X::X(const X&) (" << throws_ << ")" << endl;
  }

  ~X()
  {
    cerr << "X::~X() (" << throws_ << ")" << endl;
    if (throws_) { throw 1; }
  }
private:
  bool throws_;
};

X f()
{
  X x(true);
  return X(false);
}

int main()
{
  try
  {
    f();
  }
  catch (...)
  {
    cerr << "Catch!" << endl;
  }
}
---------- end of etest.cc -----------
pulu:/home/bitti/tmp/t> g++ etest.cc
pulu:/home/bitti/tmp/t> ./a.out
X::X() (1)
X::X() (0)
X::~X() (1)
Catch!


-- 
           Summary: Return value's destructor not executed when a local
                    variable's destructor throws
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: bitti at iki dot fi
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33799


^ permalink raw reply	[flat|nested] 24+ messages in thread
[parent not found: <bug-33799-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2024-01-24 19:40 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-17 11:31 [Bug c++/33799] New: Return value's destructor not executed when a local variable's destructor throws bitti at iki dot fi
2007-10-17 12:03 ` [Bug c++/33799] " rguenth at gcc dot gnu dot org
2007-10-17 12:21 ` bitti at iki dot fi
2007-10-17 17:07 ` jason at gcc dot gnu dot org
2008-09-19 12:14 ` bitti at iki dot fi
     [not found] <bug-33799-4@http.gcc.gnu.org/bugzilla/>
2013-11-28 11:18 ` redi at gcc dot gnu.org
2013-11-28 12:15 ` temporal at gmail dot com
2020-04-20  3:30 ` mm-nospam at outlook dot co.nz
2020-04-20  9:24 ` redi at gcc dot gnu.org
2020-05-07 11:56 ` jakub at gcc dot gnu.org
2020-07-23  6:52 ` rguenth at gcc dot gnu.org
2021-04-08 12:02 ` rguenth at gcc dot gnu.org
2022-01-06 14:31 ` jason at gcc dot gnu.org
2022-01-07  0:26 ` cvs-commit at gcc dot gnu.org
2022-02-01 22:51 ` jason at gcc dot gnu.org
2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
2023-11-02 20:01 ` cvs-commit at gcc dot gnu.org
2023-11-17  0:21 ` cvs-commit at gcc dot gnu.org
2023-11-17  0:21 ` cvs-commit at gcc dot gnu.org
2023-11-17  0:21 ` cvs-commit at gcc dot gnu.org
2023-11-17  0:21 ` cvs-commit at gcc dot gnu.org
2023-12-20 17:31 ` cvs-commit at gcc dot gnu.org
2024-01-24 19:39 ` cvs-commit at gcc dot gnu.org
2024-01-24 19:40 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).