I''m using a multidimensional array to store the tiles for a map, in my map editor. I have working code to use a single dimensional array to store the tiles, but I read the thread about dynamic allocation, and decided to try it. Unfortunately, I cannot seem to delete the array!
Here''s how I allocate the array:
// Create a new array of tile and object pointers
m_pTiles = (Tile***) malloc(dwTiles * (sizeof( Tile* )));
// Fill the array with new tile objects
for( int x=0; x < m_nWidth; x++ )
for( int y=0; y < m_nHeight; y++ )
m_pTiles[x][y] = new Tile(x, y, this);
And here''s how I delete it:
for( int x=0; x < m_nWidth; x++ )
{
for( int y=0; y < m_nHeight; y++ )
{
delete m_pTiles[x][y]; // crashes here, when x=1 and y=0
m_pTiles[x][y] = NULL;
}
}
free(m_pTiles);
m_pTiles = NULL;
As commented in the previous code section, the for loops delete the first column, and then crashes. In the debugger, m_pTiles[0][0] has the same address as m_pTiles[1][0]!! So, I would assume that something is wrong with my for loops. This is so frustrating! Thanks in advance for responding
Thanks!
- null_pointer