[llvm-bugs] [Bug 51523] New: Missing returned object destructor call after exception throw

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Aug 18 08:20:00 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51523

            Bug ID: 51523
           Summary: Missing returned object destructor call after
                    exception throw
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: mbtrash at yandex.ru
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

14.3.2 Exception handling:Constructors and destructors:2
(https://eel.is/c++draft/except.ctor#2)
says
"If an exception is thrown during the destruction of temporaries or local
variables for a return statement ([stmt.return]), the destructor for the
returned object (if any) is also invoked"

And it contains example (https://eel.is/c++draft/except.ctor#example-1) with
description
"...the local variable y is destroyed, causing stack unwinding, resulting in
the destruction of the returned object, ..."

Trying to recreate this example with following code

#include <iostream>
using namespace std;

struct S{
  int i = 123;
  S(int i) :i(i) {cout<<"S("<<i<<")"<<endl;}
  ~S() {cout<<"~S("<<i<<")"<<endl;}
};

struct T {
  T() {cout<<"T()"<<endl;}
  ~T() noexcept(false) {
    cout<<"~T()"<<endl;
    throw 0;
  }
};

S foo() {
  try {
    T t;
    return {3};
  } catch (...){}
  return {4};
}

int main() {
  foo();
}

Output doesn't have S{3}'s (returned object) destruction line:
T()
S(3)
~T()
S(4)
~S(4)

while should be
T()
S(3)
~T()
~S(3) <-----!
S(4)
~S(4)

Link to godbolt:
https://godbolt.org/z/YrasqsvMe

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210818/058bbe00/attachment.html>


More information about the llvm-bugs mailing list