From 9f2a850a95267a38e953bbdafbdfb9a2659a61bf Mon Sep 17 00:00:00 2001 From: historic_bruno Date: Tue, 17 Sep 2013 00:28:22 +0000 Subject: [PATCH] Fixes buggy operators and memory leak in Grid class, refs #1842 This was SVN commit r13866. --- source/simulation2/helpers/Grid.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/source/simulation2/helpers/Grid.h b/source/simulation2/helpers/Grid.h index fc087a2f46..c2a607982b 100644 --- a/source/simulation2/helpers/Grid.h +++ b/source/simulation2/helpers/Grid.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2011 Wildfire Games. +/* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -48,7 +48,16 @@ public: Grid(const Grid& g) { - *this = g; + m_W = g.m_W; + m_H = g.m_H; + m_DirtyID = g.m_DirtyID; + if (g.m_Data) + { + m_Data = new T[m_W * m_H]; + memcpy(m_Data, g.m_Data, m_W*m_H*sizeof(T)); + } + else + m_Data = NULL; } Grid& operator=(const Grid& g) @@ -60,8 +69,10 @@ public: m_DirtyID = g.m_DirtyID; if (g.m_Data) { - m_Data = new T[m_W * m_H]; - memcpy(m_Data, g.m_Data, m_W*m_H*sizeof(T)); + T* ptr = new T[m_W * m_H]; + memcpy(ptr, g.m_Data, m_W*m_H*sizeof(T)); + delete[] m_Data; + m_Data = ptr; } else m_Data = NULL;