https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117673
Bug ID: 117673
Summary: Unused static objects with non-trivial destructors not
removed
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: rdiez-2006 at rd10 dot de
Target Milestone: ---
While investigating bug report 117630, I noticed that GCC is sometimes not
smart enough to remove unused static objects if they have a non-trivial
destructor.
I have created a test case here:
https://godbolt.org/z/a1a4GrbE8
I initially saw this problem with std::string, but the implementation is rather
complicated, and I didn't know what was causing it, so I thought I could try to
reproduce it with malloc/free pairs which have no effect and can be optimised
away.
I mean that GCC can apparently optimise this statement completely away:
free( malloc( 456 ) );
That optimisation works even if the malloc() call is in the constructor, and
the corresponding free() call in the destructor, but not if the object happens
to be a static instance.