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<iostream>
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
...