[Boost-bugs] [Boost C++ Libraries] #7114: Destructor not called when using emplace()

Subject: [Boost-bugs] [Boost C++ Libraries] #7114: Destructor not called when using emplace()
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-07-10 14:40:35


#7114: Destructor not called when using emplace()
---------------------------------------------------------+------------------
 Reporter: Alexandre Hamez <alexandre.hamez@…> | Owner: igaztanaga
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: container
  Version: Boost 1.50.0 | Severity: Problem
 Keywords: |
---------------------------------------------------------+------------------
 Consider the following source code:
 {{{
 #include <iostream>
 #include <set>

 #include <boost/container/flat_set.hpp>

 struct foo
 {
   int i_;

   foo(int i)
     : i_(i)
   {
     std::cout << "foo() " << this << std::endl;
   }

   ~foo()
   {
     std::cout << "~foo() " << this << std::endl;
   }

   foo(foo&& other)
     : i_(other.i_)
   {
     std::cout << "foo(foo&&) " << this << std::endl;
   }

   foo&
   operator=(foo&& other)
   {
     std::cout << "foo& operator=(foo&&)" << this << std::endl;
     i_ = other.i_;
     return *this;
   }

   bool
   operator==(const foo& other)
   const
   {
     return i_ == other.i_;
   }

   bool
   operator<(const foo& other)
   const
   {
     return i_ < other.i_;
   }
 };

 int
 main()
 {
   boost::container::flat_set<foo> set;
   // std::set<foo> set;
   std::cout << "*************" << std::endl;
   set.emplace(42);
   std::cout << "*************" << std::endl;
   set.emplace(42);
   std::cout << "*************" << std::endl;
   return 0;
 }

 }}}

 When compiled with clang 3.1 or gcc 4.8 and launched, it displays:

 {{{
 *************
 foo() 0x7fff6bfb29f8
 foo(foo&&) 0x10c400900
 *************
 foo() 0x7fff6bfb29f8
 *************
 ~foo() 0x10c400900
 }}}

 Three instances are created (two constructors and one move-contructor),
 but only one destructor is called.
 As a side note, when using an {{{std::set}}} (of the libc++) instead of a
 {{{boost::container::flat_set}}}, it displays:

 {{{
 *************
 foo() 0x10490091c
 *************
 foo() 0x10490093c
 ~foo() 0x10490093c
 *************
 ~foo() 0x10490091c
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/7114>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:10 UTC