Opened 10 years ago

Closed 10 years ago

#7114 closed Bugs (fixed)

Destructor not called when using emplace()

Reported by: Alexandre Hamez <alexandre.hamez@…> Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: container
Version: Boost 1.50.0 Severity: Problem
Keywords: Cc:

Description

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

Change History (1)

comment:1 by Ion Gaztañaga, 10 years ago

Resolution: fixed
Status: newclosed

Thanks for the report, fixed in trunk at revision: 79422

Note: See TracTickets for help on using tickets.