Issue 124306
Summary destruction order not respected
Labels new issue
Assignees
Reporter mrussoLuxoft
    This web page:

[cppquiz](https://cppquiz.org/quiz/question/323?result=OK&answer=bcad&did_answer=Answer)

shows an error of Clang about destruction order in case of exception while destructing local objects after having constructed the returned object.

It seems no one reported yet to Clang, is it ?


The code recalls an example from the standard, in [ [except.ctor]-p2 ](https://timsong-cpp.github.io/cppwp/std23/except.ctor#2).

I have elaborated a bit more with some std::cout and some few additional stuff, here: [godbolt](https://godbolt.org/z/a454Ge5oY)



Original code is:

struct A { };

struct Y { ~Y() noexcept(false) { throw 0; } };

A f() {
  try {
    A a;
    Y y;
    A b;
    return {};      // #1
  } catch (...) {
  }
  return {};        // #2
}

and the problem is that after the exception throw by y-destruction, the returned object constructed in #1 is not destroyed, also determining a memory leak as shown on godbolt link.



My code elaboration is:

#include <iostream>
#include <stdexcept>

unsigned d = 0;
struct A {
    A(char c) : c_(c), p(new int()) {std::cout << "constr. " << c_ << '\n';}
    ~A() { std::cout << "destr. " << c_ << '\n'; delete p;}
    char c_;
    int *p;
};

struct Y { ~Y() noexcept(false) { throw std::runtime_error(""); } };

A f() {
    try {
        A a('a');
        Y y;
        A b('b');
        return {'c'};
    } catch (...) {
    }
    return {'d'};
}

int main()
{
    A x = f();
}
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to