From 06779d249d55188d0404fae75c08f7b9164b317d Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Wed, 25 Apr 2018 22:27:57 +0200 Subject: [PATCH] fix memory leak in ptr vector and array --- alib2std/src/extensions/ptr_array.hpp | 8 +++----- alib2std/src/extensions/ptr_vector.hpp | 10 ++++------ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/alib2std/src/extensions/ptr_array.hpp b/alib2std/src/extensions/ptr_array.hpp index 4d88d22f41..cd16cae880 100644 --- a/alib2std/src/extensions/ptr_array.hpp +++ b/alib2std/src/extensions/ptr_array.hpp @@ -207,20 +207,18 @@ public: template < class R > iterator set ( const_iterator pos, R && value ) { - m_data.at ( std::distance ( cbegin ( ), pos ) ) = ext::clone ( std::forward < R > ( value ) ); - return dereferencer ( m_data.begin ( ) + std::distance ( cbegin ( ), pos ) ); + return set ( pos, ext::clone ( std::forward < R > ( value ) ) ); } - template < class R > iterator set ( const_iterator pos, T * value ) { + delete m_data.at ( std::distance ( cbegin ( ), pos ) ); m_data.at ( std::distance ( cbegin ( ), pos ) ) = value ; return dereferencer ( m_data.begin ( ) + std::distance ( cbegin ( ), pos ) ); } template < class R, class ... Args > iterator emplace_set ( const_iterator pos, Args && ... args ) { - m_data.at ( std::distance ( cbegin ( ), pos ) ) = new R ( std::forward < Args > ( args ) ... ) ; - return dereferencer ( m_data.begin ( ) + std::distance ( cbegin ( ), pos ) ); + return set ( pos, new R ( std::forward < Args > ( args ) ... ) ); } void swap ( ptr_array & other ) { diff --git a/alib2std/src/extensions/ptr_vector.hpp b/alib2std/src/extensions/ptr_vector.hpp index b0e0fcb70c..67211a66fc 100644 --- a/alib2std/src/extensions/ptr_vector.hpp +++ b/alib2std/src/extensions/ptr_vector.hpp @@ -236,20 +236,18 @@ public: template < class R > iterator set ( const_iterator pos, R && value ) { - m_data.at ( std::distance ( cbegin ( ), pos ) ) = ext::clone ( std::forward < R > ( value ) ); - return dereferencer ( m_data.begin ( ) + std::distance ( cbegin ( ), pos ) ); + return set ( pos, ext::clone ( std::forward < R > ( value ) ) ); } - template < class R > iterator set ( const_iterator pos, T * value ) { - m_data.at ( std::distance ( cbegin ( ), pos ) ) = value ; + delete m_data.at ( std::distance ( cbegin ( ), pos ) ); + m_data.at ( std::distance ( cbegin ( ), pos ) ) = value; return dereferencer ( m_data.begin ( ) + std::distance ( cbegin ( ), pos ) ); } template < class R, class ... Args > iterator emplace_set ( const_iterator pos, Args && ... args ) { - m_data.at ( std::distance ( cbegin ( ), pos ) ) = new R ( std::forward < Args > ( args ) ... ) ; - return dereferencer ( m_data.begin ( ) + std::distance ( cbegin ( ), pos ) ); + return set ( pos, new R ( std::forward < R > ( args ) ... ) ); } template < class R > -- GitLab