| Issue |
182019
|
| Summary |
[clang] Clang skips the destructor for a fully constructed object on some exception flows
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
vinipsmaker
|
I guess the problem is related to copy elision generating new code branches, and the exception flow not taking this into account, but that's just a hunch. Here's the code:
```cpp
#include <print>
static int counter;
struct T
{
T()
{
std::println("ctor: {}", id);
}
T(const T&) = delete;
T& operator=(const T&) = delete;
T(bool should_throw)
: should_throw{should_throw}
{
std::println("ctor: {}", id);
}
T(T&& o)
: should_throw{o.should_throw}
{
std::println("ctor: {}", id);
o.should_throw = false;
}
~T() noexcept(false)
{
std::println("dtor: {}", id);
if (should_throw) {
throw id;
}
}
bool should_throw = false;
int id = counter++;
};
T f()
{
T t2{true};
T t1{false};
return t1;
}
int main()
{
try {
T t = f();
std::println("value: {}", t.id);
} catch (int i) {
std::println("exception: {}", i);
}
}
```
Output of this program when compiled with GCC:
```
ctor: 0
ctor: 1
dtor: 0
dtor: 1
exception: 0
```
Output of this program when compiled with clang:
```
ctor: 0
ctor: 1
dtor: 0
exception: 0
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs