Archived

This topic is now archived and is closed to further replies.

Mike737

Deleting an array of classes

Recommended Posts

Mike737    122
What is wrong with my following code? When it comes time to call ResetMap() after I have called CreateBlankMap() I recieve a memory issue "Unhandled exception at 0x77f7f570 (ntdll.dll) in Map Editor.exe: User breakpoint." on delete []m_poTile;. How can I get it to free up my memory!
class CMap
{
...........
private:
CTile *m_poTile[MAP_MAX_TILES][MAP_MAX_TILES];
...........
};

void CMap::ResetMap()
{
	delete []m_poTile;
}

void CMap::CreateBlankMap(const int iX, const int iY)
{
	m_iHeight = iY;
	m_iWidth = iX;

	for (int iTileX = 0; iTileX < m_iWidth; iTileX++)
	{
		for (int iTileY = 0; iTileY < m_iHeight; iTileY++)
		{
			m_poTile[iTileX][iTileY] = new CTile(iTileX,iTileY);
		}
	}
}
---- Mike Portfolio: Http://members.iinet.net.au/~slyons Team AI: Http://members.iinet.net.au/~slyons/teamai

Share this post


Link to post
Share on other sites
stubble    158
m_poTile is an array of pointers. You have created the array on the stack - not the heap. You then iterate through the array creating tiles for each element of the array - using "new" - so they are created on the heap.

You getting an error because you are deleting the array which is managed on the stack - you shouldn''t delete things on the stack. You *should* however delete each tile in the array as you created them on the heap using new. So iterate through your array and delete them one at a time.

Whether this is the best way to reset your map I''m not sure.

Stubble

Share this post


Link to post
Share on other sites