diff options
Diffstat (limited to 'source/10-common/RingBuffer.hpp')
-rw-r--r-- | source/10-common/RingBuffer.hpp | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/source/10-common/RingBuffer.hpp b/source/10-common/RingBuffer.hpp index de8227c..4eaa007 100644 --- a/source/10-common/RingBuffer.hpp +++ b/source/10-common/RingBuffer.hpp @@ -72,7 +72,7 @@ public: RingBuffer() noexcept = default; ~RingBuffer() noexcept { - delete mRing; + delete[] mRing; } RingBuffer(const RingBuffer&) noexcept = delete; @@ -128,6 +128,7 @@ public: [[nodiscard]] T& operator[](size_type i) { return const_cast<T&>(const_cast<const RingBuffer&>(*this)[i]); } [[nodiscard]] const T& operator[](size_type i) const { + assert(mRing != nullptr); size_type idx = mHeadIdx + i; if (idx >= mCapacity) { idx -= mCapacity; @@ -136,6 +137,7 @@ public: } void push_back(T t) { + assert(mRing != nullptr); if (mTailIdx == mCapacity) { // Ring buffer is filled to the right, warp around to the beginning // mHeadIdx > 0 must be true, since we checked that as condition (1) above @@ -177,8 +179,10 @@ public: auto oldRing = mRing; auto newRing = mRing = new T[newCapacity]; - std::rotate_copy(oldRing, oldRing + mHeadIdx, oldRing + mCapacity, newRing); - delete oldRing; + if (oldRing != nullptr) { + std::rotate_copy(oldRing, oldRing + mHeadIdx, oldRing + mCapacity, newRing); + delete[] oldRing; + } mCapacity = newCapacity; mHeadIdx = 0; |