From de45f0896feeb257b2a6d9b41fdbab9ddadb3cfb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 03:28:29 -0400 Subject: [PATCH] MemoryWriter: Use a std::unique_ptr within resize() Same behavior, but keeps the memory managed throughout its whole lifetime. --- src/athena/MemoryWriter.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/athena/MemoryWriter.cpp b/src/athena/MemoryWriter.cpp index a7e3c68..607a964 100644 --- a/src/athena/MemoryWriter.cpp +++ b/src/athena/MemoryWriter.cpp @@ -260,15 +260,14 @@ void MemoryCopyWriter::resize(atUint64 newSize) { } // Allocate and copy new buffer - atUint8* newArray = new atUint8[newSize]; - memset(newArray, 0, newSize); - - if (m_dataCopy) - memmove(newArray, m_dataCopy.get(), m_length); - m_dataCopy.reset(newArray); + auto newArray = std::make_unique(newSize); + if (m_dataCopy) { + std::memmove(newArray.get(), m_dataCopy.get(), m_length); + } + m_dataCopy = std::move(newArray); // Swap the pointer and size out for the new ones. - m_data = newArray; + m_data = m_dataCopy.get(); m_length = newSize; }