[Bug c++/92407] New: Destruction of objects returned from functions skipped by goto
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92407 Bug ID: 92407 Summary: Destruction of objects returned from functions skipped by goto Product: gcc Version: 9.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: Dave.Poston at gs dot com Target Milestone: --- The code below only outputs 'Destructed' once: Godbolt link: https://godbolt.org/z/Foy-vc Bug appears to be present in all versions of GCC available in godbolt. --- #include struct MyStruct { MyStruct() { std::cout << "Constructed" << std::endl; } ~MyStruct() { std::cout << "Destructed" << std::endl; } int a; }; MyStruct fill() { MyStruct bla; bla.a = 100; return bla; } MyStruct problem() { int cnt = 100; start: MyStruct a = fill(); if( cnt-- ) goto start; return a; } int main( int argc, char** argv ) { problem(); return 0; } --- output is: Constructed Constructed Constructed Constructed Constructed Constructed Constructed Constructed ...
[Bug c++/92407] Destruction of objects returned from functions skipped by goto
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92407 --- Comment #1 from Dave Poston --- In fact, fill() isn't even needed. Smaller repro: https://godbolt.org/z/ubXl7Y --- #include struct MyStruct { MyStruct() { std::cout << "Constructed" << std::endl; } ~MyStruct() { std::cout << "Destructed" << std::endl; } int a; }; MyStruct problem() { int cnt = 100; start: MyStruct a; if( cnt-- ) goto start; return a; } int main( int argc, char** argv ) { problem(); return 0; }
[Bug c++/92407] Destruction of objects returned from functions skipped by goto
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92407 --- Comment #4 from Dave Poston --- Yep, also changing problem()/foo() to void and not returning the struct, works properly