From 478f96d0fd36033d2c2b2451ce951054f957babb Mon Sep 17 00:00:00 2001 From: wraitii Date: Sat, 31 Oct 2020 10:21:08 +0000 Subject: [PATCH] Fix Atlas crash in RangeManager following 939002f0dc 939002f0dc changed from vectors to grid which broke resetting when terrain size changed. Also use SAFE_ARRAY_DELETE for simplicity. Reported by: vladislavbelov Reviewed By: vladislavbelov Differential Revision: https://code.wildfiregames.com/D2961 This was SVN commit r24117. --- .../components/CCmpRangeManager.cpp | 2 +- source/simulation2/helpers/Grid.h | 27 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/source/simulation2/components/CCmpRangeManager.cpp b/source/simulation2/components/CCmpRangeManager.cpp index 59344db621..cd4b656ad6 100644 --- a/source/simulation2/components/CCmpRangeManager.cpp +++ b/source/simulation2/components/CCmpRangeManager.cpp @@ -834,7 +834,7 @@ public: m_LosTilesPerSide = (m_TerrainVerticesPerSide - 1)/LOS_TILES_RATIO; for (size_t player_id = 0; player_id < m_LosPlayerCounts.size(); ++player_id) - m_LosPlayerCounts[player_id].reset(); + m_LosPlayerCounts[player_id].clear(); m_ExploredVertices.clear(); m_ExploredVertices.resize(MAX_LOS_PLAYER_ID+1, 0); diff --git a/source/simulation2/helpers/Grid.h b/source/simulation2/helpers/Grid.h index 7e0dc10076..3932913a5a 100644 --- a/source/simulation2/helpers/Grid.h +++ b/source/simulation2/helpers/Grid.h @@ -94,14 +94,12 @@ public: m_W = g.m_W; m_H = g.m_H; - delete[] m_Data; + SAFE_ARRAY_DELETE(m_Data); if (g.m_Data) { m_Data = new T[m_W * m_H]; copy_data(g.m_Data, dispatch{}); } - else - m_Data = NULL; return *this; } @@ -114,7 +112,7 @@ public: ~Grid() { - delete[] m_Data; + SAFE_ARRAY_DELETE(m_Data); } // Ensure that o and this are the same size before calling. @@ -168,20 +166,29 @@ public: void reset_data(default_type) { std::fill(&m_Data[0], &m_Data[m_H*m_W], T{}); } void reset_data(is_pod) { memset(m_Data, 0, m_W*m_H*sizeof(T)); } + // Reset the data to its default-constructed value (usually 0), not changing size. void reset() { if (m_Data) reset_data(dispatch{}); } + // Clear the grid setting the size to 0 and freeing any data. + void clear() + { + resize(0, 0); + } + void resize(u16 w, u16 h) { - if (m_Data) - delete[] m_Data; + SAFE_ARRAY_DELETE(m_Data); m_W = w; m_H = h; - if (m_W || m_H) - m_Data = new T[m_W * m_H]; + + if (!m_W && !m_H) + return; + + m_Data = new T[m_W * m_H]; ENSURE(m_Data); reset(); } @@ -337,13 +344,13 @@ public: ~SparseGrid() { reset(); - delete[] m_Data; + SAFE_ARRAY_DELETE(m_Data); } void reset() { for (size_t i = 0; i < (size_t)(m_BW*m_BH); ++i) - delete[] m_Data[i]; + SAFE_ARRAY_DELETE(m_Data[i]); // Reset m_Data by value-constructing in place with placement new. m_Data = new (m_Data) T*[m_BW*m_BH]();