diff --git a/storm/array/TSFixedArray.hpp b/storm/array/TSFixedArray.hpp index 0435f2c..4f39d51 100644 --- a/storm/array/TSFixedArray.hpp +++ b/storm/array/TSFixedArray.hpp @@ -60,11 +60,20 @@ void TSFixedArray::Clear() { template void TSFixedArray::ReallocAndClearData(uint32_t count) { - this->m_alloc = count; + // Destruct existing array elements + for (uint32_t i = 0; i < this->Count(); i++) { + auto element = &this->operator[](i); + element->~T(); + } - if (this->m_data || count) { - void* m = SMemReAlloc(this->m_data, sizeof(T) * count, this->MemFileName(), this->MemLineNo(), 0x0); - this->m_data = static_cast(m); + // Reallocate if count changed + if (count != this->m_count) { + this->m_alloc = count; + + if (this->m_data || count) { + void* m = SMemReAlloc(this->m_data, sizeof(T) * count, this->MemFileName(), this->MemLineNo(), 0x0); + this->m_data = static_cast(m); + } } }