https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82560
Bug ID: 82560 Summary: Temporary object created as a default argument not destructed Product: gcc Version: 7.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: gdutor at gmail dot com Target Milestone: --- Created attachment 42374 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=42374&action=edit Source code that reproduce the bug This is the code trigger the leaking bug. ===================================================== #include <iostream> #include <vector> #include <memory> class Foo { public: Foo(const char *str) : str_(str) { std::cout << "Foo(const char*)" << std::endl; } Foo(const Foo&) { std::cout << "Foo(const Foo&)" << std::endl; } ~Foo() { std::cout << "~Foo()" << std::endl; } std::string str_; }; class Bar { public: Bar(Foo foo = "implicitly casted to Foo") : foo_(foo) { std::cout << "Bar(Foo)" << std::endl; } ~Bar() { std::cout << "~Bar()" << std::endl; } Foo foo_; }; int main() { auto ptr = std::make_unique<Bar>(); return 0; } ===================================================== Compile and run it: ================= $ g++ -std=c++14 leak.cpp $ ./a.out Foo(const char*) Foo(const Foo&) Bar(Foo) ~Bar() ~Foo() ================= As the outputs indicate, two Foo objects are created, while the destructor of Foo invoked only once. Here is what g++ -v prints: ================= Using built-in specs. COLLECT_GCC=third-party/gcc-7.1.0/glibc-2.17/bin/g++ COLLECT_LTO_WRAPPER=/data/admin/Workspace/third-party/gcc-7.1.0/glibc-2.17/bin/../libexec/gcc/x86_64-pc-linux-gnu/7.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: third-party/gcc-7.1.0/source/gcc-7.1.0/configure --prefix=third-party/gcc-7.1.0/glibc-2.17 --with-gmp=third-party/gcc-7.1.0/../gmp-6.1.2/glibc-2.17 --with-mpfr=third-party/gcc-7.1.0/../mpfr-3.1.6/glibc-2.17 --with-mpc=third-party/gcc-7.1.0/../mpc-1.0.3/glibc-2.17 --without-isl --enable-static-libjava --disable-multiarch --disable-multilib --enable-languages=c,c++,go --disable-libada --disable-libvtv --enable-default-pie --disable-nls --disable-lto Thread model: posix gcc version 7.1.0 (GCC) =================