Skip to content

Object is not destructed while stack-unwinding #67074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DKay7 opened this issue Sep 21, 2023 · 5 comments
Closed

Object is not destructed while stack-unwinding #67074

DKay7 opened this issue Sep 21, 2023 · 5 comments
Labels
c++ clang:codegen IR generation bugs: mangling, exceptions, etc. duplicate Resolved as duplicate

Comments

@DKay7
Copy link
Contributor

DKay7 commented Sep 21, 2023

I was solving cpp-quiz and got question with followed code:

#include <iostream>
#include <stdexcept>

struct A {
    A(char c) : c_(c) {}
    ~A() { std::cout << c_; }
    char c_;
};

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()
{
    f();
}

According to standard, program's output should be bcad. But clang-18 produces just bad. I performed some tests and found out that A-object with 'c' value was never destructed. Order of construction and destruction that I get is:

a CONSTRUCTED
b CONSTRUCTED
c CONSTRUCTED
b DESTRUCTED
a DESTRUCTED
d CONSTRUCTED
d DESTRUCTED
@DKay7 DKay7 changed the title Object destruction while stack-unwinding are not accorded to standard Object is not destructed while stack-unwinding Sep 22, 2023
@Endilll Endilll added clang Clang issues not falling into any other category c++ confirmed Verified by a second party and removed new issue labels Sep 25, 2023
@Endilll
Copy link
Contributor

Endilll commented Sep 25, 2023

Confirmed: https://godbolt.org/z/Eo9hzjPhe
I feel like that it's not the first filed bug about (lack of) destruction of temporaries during unwinding, but I can't find other issues.

@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2023

@llvm/issue-subscribers-c-1

I was solving cpp-quiz and got [question](https://cppquiz.org/quiz/question/323?result=OK&answer=bcad&did_answer=Answer) with followed code:
#include &lt;iostream&gt;
#include &lt;stdexcept&gt;

struct A {
    A(char c) : c_(c) {}
    ~A() { std::cout &lt;&lt; c_; }
    char c_;
};

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()
{
    f();
}

According to standard, program's output should be bcad. But clang-18 produces just bad. I performed some tests and found out that A-object with 'c' value was never destructed. Order of construction and destruction that I get is:

a CONSTRUCTED
b CONSTRUCTED
c CONSTRUCTED
b DESTRUCTED
a DESTRUCTED
d CONSTRUCTED
d DESTRUCTED

@shafik
Copy link
Collaborator

shafik commented Sep 25, 2023

Useful to note this example comes from except.ctor p2.

I don't know where we would need to fix this but I am hoping @zygoloid does

NB: I think the last lifetimes issue I saw was: #65282

@zygoloid
Copy link
Collaborator

Duplicate of #12658

@zygoloid zygoloid marked this as a duplicate of #12658 Sep 25, 2023
@zygoloid zygoloid closed this as not planned Won't fix, can't repro, duplicate, stale Sep 25, 2023
@Endilll Endilll added duplicate Resolved as duplicate clang:codegen IR generation bugs: mangling, exceptions, etc. and removed clang Clang issues not falling into any other category confirmed Verified by a second party labels Sep 26, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 26, 2023

@llvm/issue-subscribers-clang-codegen

I was solving cpp-quiz and got [question](https://cppquiz.org/quiz/question/323?result=OK&answer=bcad&did_answer=Answer) with followed code:
#include &lt;iostream&gt;
#include &lt;stdexcept&gt;

struct A {
    A(char c) : c_(c) {}
    ~A() { std::cout &lt;&lt; c_; }
    char c_;
};

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()
{
    f();
}

According to standard, program's output should be bcad. But clang-18 produces just bad. I performed some tests and found out that A-object with 'c' value was never destructed. Order of construction and destruction that I get is:

a CONSTRUCTED
b CONSTRUCTED
c CONSTRUCTED
b DESTRUCTED
a DESTRUCTED
d CONSTRUCTED
d DESTRUCTED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ clang:codegen IR generation bugs: mangling, exceptions, etc. duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

5 participants