This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/66451] New: Array not fully destructed if element destructor throws exception


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66451

            Bug ID: 66451
           Summary: Array not fully destructed if element destructor
                    throws exception
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jonathandodd at gmail dot com
  Target Milestone: ---

If an array element throws during destruction, undestructed elements in that
array are not destructed. Other local variables are destructed. Clang calls
destructors on all automatic objects, regardless whether in or out of an array.

(And yes, I know throwing from destructors should be avoided, but that's hardly
the point ;p)

Relevant sections from standard
-------------------------------

s15.2:

As control passes from the point where an exception is thrown to a handler,
destructors are invoked for all automatic objects constructed since the try
block was entered. ...

An object of any storage duration whose initialization or destruction is
terminated by an exception will have destructors executed for all of its fully
constructed subobjects

s1.8:

A subobject can be ... an array element.


Test case
---------
#include <iostream>
#include <exception>

using namespace std;

class A {
public:
  A(int new_a) : a(new_a) { }

  ~A() noexcept(false) {
    cout << "a" << a <<".~A() ";

    if(std::uncaught_exception())
      cout << "Unwinding";

    cout << endl;

    if(a==4)
      throw a;
  } 

  int a;
};

int main()
{
  try {
    A a1(1), a2(2);
    A arr[] = {3,4,5};

  } catch(...) { }
}

Output (g++ 4.9.2)
------------------

a5.~A() 
a4.~A() 
a2.~A() Unwinding
a1.~A() Unwinding


Output (clang 3.5.0)
--------------------
a5.~A() 
a4.~A() 
a3.~A() Unwinding
a2.~A() Unwinding
a1.~A() Unwinding


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]