mercredi 31 janvier 2018

Why libstdC++'s std::vector's ctor implementation doesn't occur memory leak?

I'm using GCC 7.3.0 with libstdc++.

Here is std::vector's ctor implementation.

      vector(initializer_list<value_type> __l,
         const allocator_type& __a = allocator_type())
      : _Base(__a)
      {
    _M_range_initialize(__l.begin(), __l.end(),
                random_access_iterator_tag());
      }

      // Called by the second initialize_dispatch above
      template<typename _ForwardIterator>
    void
    _M_range_initialize(_ForwardIterator __first,
                _ForwardIterator __last, std::forward_iterator_tag)
    {
      const size_type __n = std::distance(__first, __last);
      this->_M_impl._M_start = this->_M_allocate(__n);
      this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
      this->_M_impl._M_finish =
        std::__uninitialized_copy_a(__first, __last,
                    this->_M_impl._M_start,
                    _M_get_Tp_allocator());
    }

In function _M_range_initialize(_ForwardIterator,_ForwardIterator,std::forward_iterator_tag), when std::__uninitialized_copy_a throw exception, this->_M_impl._M_start, that was allocated in this function, will not released, I think.

That means this implementaion will occur memory leak.

However, libstdc++ is well-tested, well-known library. My understand must be incorrect.

Why this implementation doesn't occur memory leak?

Aucun commentaire:

Enregistrer un commentaire