19

I have a class with deleted copy ctor and a destructor that frees a C resource.

I would expect emplace_back to move objects and call destructor only once, but it's being called in emplace_back, as seen in the stl implementation on linux below. Why is this happening?

The result is that the C resource is being freed more than once.

    statement(statement&&) = default;                                                 
    statement& operator=(statement&&) = default;                                      

   private:                                                                           
    statement(const statement&) = delete;                                             
    statement& operator=(const statement&) = delete;




396│   template<typename _Tp, typename _Alloc>
397│     template<typename... _Args>
398│       void
399│       vector<_Tp, _Alloc>::
400│       _M_emplace_back_aux(_Args&&... __args)
401│       {
402│         const size_type __len =
403│           _M_check_len(size_type(1), "vector::_M_emplace_back_aux");
404│         pointer __new_start(this->_M_allocate(__len));
405│         pointer __new_finish(__new_start);
406│         __try
407│           {
408│             _Alloc_traits::construct(this->_M_impl, __new_start + size(),
409│                                      std::forward<_Args>(__args)...);
410│             __new_finish = 0;
411│
412│             __new_finish
413│               = std::__uninitialized_move_if_noexcept_a
414│               (this->_M_impl._M_start, this->_M_impl._M_finish,
415│                __new_start, _M_get_Tp_allocator());
416│
417│             ++__new_finish;
418│           }
419│         __catch(...)
420│           {
421│             if (!__new_finish)
422│               _Alloc_traits::destroy(this->_M_impl, __new_start + size());
423│             else
424│               std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
425│             _M_deallocate(__new_start, __len);
426│             __throw_exception_again;
427│           }
428├>        std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
429│                       _M_get_Tp_allocator());
430│         _M_deallocate(this->_M_impl._M_start,
431│                       this->_M_impl._M_end_of_storage
432│                       - this->_M_impl._M_start);
433│         this->_M_impl._M_start = __new_start;
434│         this->_M_impl._M_finish = __new_finish;
6
  • This answer is related to my problem: stackoverflow.com/a/12739939/72784
    – piotr
    Feb 8, 2014 at 13:13
  • 1
    What if you add some noexcept on your move ctor? Would that help?
    – detunized
    Feb 8, 2014 at 13:23
  • This looks like a resizing function of vector, i.e. it allocates a new buffer, copies/moves the element from the old buffer to the new buffer, and destroyes the elements in the old buffer (but not in the new buffer). Make sure your move constructor correctly transfers the resource and leaves the moved-from object either with a new resource or w/o any resource, so that it can be destroyed w/o affecting the moved-to object.
    – dyp
    Feb 8, 2014 at 13:39
  • 1
    @detunized vector will copy only if the move ctor is not noexcept and if the copy ctor can be used (if the type is CopyInsertable). For the type statement, the copy ctor is private, so the move ctor will be used (with a weaker exception safety guarantee).
    – dyp
    Feb 8, 2014 at 13:49
  • @detunized the move ctor was default which means that is noexcept. Also the code path through STL indicated that objects were being moved. Move ctor was deleted.
    – piotr
    Feb 8, 2014 at 15:30

1 Answer 1

14

There are two things that have escaped your notice:

  1. A moved-from object will still be destructed, so your move operation must transfer resources
  2. When a vector grows, it might need to re-allocate which is a 4 steps operations: acquire new storage, move-construct (or copy-construct) new elements in the new storage (from the old), destroy old elements, release old storage.

So, your problem is simply that you do not transfer resources properly; using std::unique_ptr as the basis of your custom class and you will not suffer such woes.

1
  • I fixed it by transferring the resource on the move constructor explicitly, so the destructor doesn't free it in that case.
    – piotr
    Feb 8, 2014 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.