<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/124306>124306</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
destruction order not respected
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mrussoLuxoft
</td>
</tr>
</table>
<pre>
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();
}
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVk1v4zYT_jX0ZRBDJiUrPuig2K9fFG2xl73kFFDi2GKXIlWSiuMe9rcXQ8kfyabFAg0USOIMn_l6HsoyBH20iBUrnlixW8gxds5XvR9DcL-Nb-4QF41T5-prpwOcsIFBHpGJmmXpKp7aYfhz1H-xYsf4YxfjEMjK94zvZ9PS-SPj--RFNwxRO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