diff --git a/include/clue/fast_vector.hpp b/include/clue/fast_vector.hpp index b568037..23e60ee 100644 --- a/include/clue/fast_vector.hpp +++ b/include/clue/fast_vector.hpp @@ -58,13 +58,15 @@ template struct relocate_policy { static void move_disjoint(T* dst, T* src, T* src_end) { while (src != src_end) { - new(dst++) T( std::move(*src++) ); + new(dst++) T( std::move(*src) ); + (src++)->~T(); } } static void move_fwd(T* dst, T* src, T* src_end) { while (src != src_end) { - new(dst++) T( std::move(*src++) ); + new(dst++) T( std::move(*src) ); + (src++)->~T(); } } @@ -75,7 +77,8 @@ struct relocate_policy { src += (n - 1); dst += (n - 1); while (src != src_rend) { - new(dst--) T( std::move(*src--) ); + new(dst--) T( std::move(*src) ); + (src--)->~T(); } } } @@ -352,7 +355,6 @@ class fast_vector final { relocater::move_disjoint(pb_, other.begin(), other.end()); pn_ = pb_ + n; } - other.destroy(); other.reset(); } } @@ -591,7 +593,7 @@ class fast_vector final { relocater::move_disjoint(ss_.begin(), pb_, pn_); // release memory - alloc_.deallocate(pb_, n); + alloc_.deallocate(pb_, cur_cap); // set pointers on static array reset(); @@ -616,14 +618,13 @@ class fast_vector final { // move elements to tmp size_type n = size(); if (n > 0) { - pe_ = pb_; relocater::move_disjoint(tmp.begin(), pb_, pb_ + n); tmp.pn_ = tmp.pb_ + n; } // release own memory if (use_dynamic()) { - alloc_.deallocate(pb_, n); + alloc_.deallocate(pb_, capacity()); } reset(); @@ -651,6 +652,7 @@ class fast_vector final { // 2. move the elements in range [pos, end) towards back // 3. set end() <- end() + n // 4. return a non-const version of pos + // after move_back, the elements/iterators in [pos, pos + n) are invalid. iterator move_back(const_iterator pos, size_type n) { iterator p = const_cast(pos); if (n > 0) { diff --git a/tests/test_fast_vector.cpp b/tests/test_fast_vector.cpp index 700f1ae..ded7899 100644 --- a/tests/test_fast_vector.cpp +++ b/tests/test_fast_vector.cpp @@ -14,7 +14,9 @@ class Val final { public: long* pv_; - explicit Val(std::nullptr_t) : pv_(nullptr) {} + explicit Val(std::nullptr_t) : pv_(nullptr) { + count_object++; + } explicit Val() : pv_(new long(init)) { count_object++; @@ -28,14 +30,16 @@ class Val final { explicit Val(Val&& r) noexcept : pv_(r.pv_) { r.pv_ = nullptr; + count_object++; } ~Val() { if (pv_) { - CLUE_ASSERT(count_object > 0); + delete pv_; - count_object--; } + CLUE_ASSERT(count_object > 0); + count_object--; } long get() const { return *pv_; }