Fix ticket #13500: Memory leak when using erase on string vectors

This commit is contained in:
Ion Gaztañaga
2018-04-04 00:31:39 +02:00
parent 318784291a
commit b3eee90a81
2 changed files with 4 additions and 2 deletions

View File

@@ -1243,6 +1243,7 @@ use [*Boost.Container]? There are several reasons for that:
* [@https://github.com/boostorg/container/pull/61 GitHub #61: ['"Compile problems on Android ndk r16 beta 1"]].
* [@https://github.com/boostorg/container/pull/64 GitHub #64: ['"Fix splice for slist"]].
* [@https://github.com/boostorg/container/issues/58 GitHub #65: ['"`pmr::monotonic_buffer_resource::allocate()` can return a pointer to freed memory after `release()` is called"]].
* [@https://svn.boost.org/trac/boost/ticket/13500 Trac #13500: ['"Memory leak when using erase on string vectors"]].
[endsect]

View File

@@ -2023,7 +2023,7 @@ class vector
T *const beg_ptr = this->priv_raw_begin();
T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
//Move elements forward and destroy last
this->priv_destroy_last(pos_ptr == new_end_ptr);
this->priv_destroy_last(pos_ptr != new_end_ptr);
return iterator(p);
}
@@ -2557,7 +2557,8 @@ class vector
void priv_destroy_last(const bool moved = false) BOOST_NOEXCEPT_OR_NOTHROW
{
(void)moved;
if(!(value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved))){
const bool skip_destructor = value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved);
if(!skip_destructor){
value_type* const p = this->priv_raw_end() - 1;
allocator_traits_type::destroy(this->get_stored_allocator(), p);
}