https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124744
Bug ID: 124744
Summary: shared_ptr<T> destructor instantiates unrelated
make_shared<U> from inline constructor never called
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: sberg.fun at gmail dot com
Target Milestone: ---
At least with both `gcc-c++-16.0.1-0.9.fc44.x86_64` and recent GCC 16 trunk
compiled from source,
```
$ cat test.cc
#include <memory>
void do_destruct(int &);
struct State {
~State() { do_destruct(handle); }
int handle = 0;
};
struct Iter {
std::shared_ptr<State> S;
explicit Iter(int x) : S(std::make_shared<State>()) { (void)x; }
};
struct Inner {
Iter it{0};
};
struct Outer {
std::shared_ptr<Inner> ptr;
~Outer() = default;
};
Outer o;
```
```
$ g++ -O2 -fPIC -shared -z defs test.cc
/usr/bin/ld.bfd: /tmp/ccmVttuA.o: in function
`std::_Sp_counted_ptr_inplace<State, std::allocator<void>,
(__gnu_cxx::_Lock_policy)2>::_M_dispose()':
test.cc:(.text._ZNSt23_Sp_counted_ptr_inplaceI5StateSaIvELN9__gnu_cxx12_Lock_policyE2EE10_M_disposeEv[_ZNSt23_Sp_counted_ptr_inplaceI5StateSaIvELN9__gnu_cxx12_Lock_policyE2EE10_M_disposeEv]+0x9):
undefined reference to `do_destruct(int&)'
/usr/bin/ld.bfd: /tmp/ccmVttuA.o: in function
`std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release_last_use_cold()':
test.cc:(.text._ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv[_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv]+0x45):
undefined reference to `do_destruct(int&)'
/usr/bin/ld.bfd: /tmp/ccmVttuA.o: in function `Outer::~Outer()':
test.cc:(.text._ZN5OuterD2Ev[_ZN5OuterD5Ev]+0xea): undefined reference to
`do_destruct(int&)'
collect2: error: ld returned 1 exit status
```
Godbolt suggests that this works fine with GCC 15.2 at least. (I ran into this
at <https://github.com/WebAssembly/binaryen/pull/8559> "Stub out
llvm::sys::fs::detail::directory_iterator_destruct for GCC 16". The workaround
there might or might not be needed in the end, depending on whether this new
GCC behavior is legitimate or a bug.)