At least with a recent GCC trunk build ("gcc (GCC) 7.0.1 20170221 (experimental)"), the below program does not print "dtor" when built with -std=c++17 (but does when built with -std=c++14, or with an older GCC, gcc-c++-6.3.1-1.fc25.x86_64). Appears that in the U ctor, a temporary shared_ptr<S> is copy-created but not destroyed. #include <iostream> #include <memory> struct S { ~S() { std::cout << "dtor\n"; } }; struct T { std::shared_ptr<S> s_; T(std::shared_ptr<S> s): s_(s) {} }; struct U { T t_; U(std::shared_ptr<S> const & s): t_(s) {} }; int main() { auto s = std::make_shared<S>(); U u(s); }
int count; struct S { S() { ++count; } S(const S&) { ++count; } ~S() { --count; } }; struct T { T(S) {} }; int main() { { S s; T u(s); } if (count) __builtin_abort(); }
Started with r245612 PR c++/78139 - destructor needed by new-expression * call.c (build_special_member_call): Use tf_no_cleanup.
Fixed.
Author: jason Date: Thu Feb 23 01:15:43 2017 New Revision: 245672 URL: https://gcc.gnu.org/viewcvs?rev=245672&root=gcc&view=rev Log: PR c++/79679 - missing destructor for argument * call.c (build_over_call): Don't pass tf_no_cleanup to argument conversions. Added: trunk/gcc/testsuite/g++.dg/init/cleanup4.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/call.c