• 14
• 12
• 9
• 10
• 9

# memory leak with STL container (vector)?

This topic is 4563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi guys i have a problem with the use of a <vector<vector<int>> and it happens when i'm shutting down my application. VisualStudion pops up a Debug Error that says Damage at: some adress and when i check it out, it is in the dbgheap.c file so i'm guessing that it's some non-deallocated memory making a wreck of things. I believe I've narrowed the problem down to the deletion of what's inside the <vector<vector<int>>, because when i comment that part out there is no problem ( and nothing in the vector ;) ) So my question is how do I delete what's inside the <vector<vector<int>> without getting any Errors?

##### Share on other sites
Chances are you have a buffer overrun that is corrupting the vector data. Check your code for pointer mixups or other memory overruns.

##### Share on other sites
I've had this problem about three times before. Are you using Visual Studio 6.0? To fix: make a new workspace and project, and copy all of your code over exactly. You do not need to change your code or any of your settings or anything. It will work, well it did for me.

##### Share on other sites
Quote:
 Original post by thallishhi guysi have a problem with the use of a > and it happens when i'm shutting down my application. VisualStudion pops up a Debug Error that says Damage at: some adress and when i check it out, it is in the dbgheap.c file so i'm guessing that it's some non-deallocated memory making a wreck of things.I believe I've narrowed the problem down to the deletion of what's inside the >, because when i comment that part out there is no problem ( and nothing in the vector ;) )

Deletion of the vector isn't the problem; it's just when the runtime notices the problem. When you deallocate a block of memory with that version of the debug runtime, it checks to see whether you've written anything past the end of that block. Your problem is most likely caused by an out-of-bounds write to one of your vectors.

BTW, a vector of vectors is almost always the wrong solution for representing 2D data.... a single vector accessed as [y*columns+x] is more memory efficient and often easier to debug.

##### Share on other sites
What exactly do you mean "the deletion of what's inside the vector< vector< int > >"? You don't need to delete the contents of the vector. The vector will handle deallocation for you. Can you post the code you're talking about?

Enigma

##### Share on other sites
Hi

It maybe some writing out-of-bounds but i can't see where it should be, maybe you guys can see. Just shout if I need to post some more code.

for (int i = 0; i < m_TileMapHeight; i++ )	{		for (int j = 0; j < m_TileMapWidth; j++ )		{			fin >> m_pTileMap[j];					}	}

##### Share on other sites
That seems fine, assuming the variables actually correspond to the actual dimensions of the vector. Either use the size() member of vector directly to get the correct dimensions or us the at() member to let the vector do bounds checking for you.

// ville

##### Share on other sites
Put in two lines before the loops

assert(m_pTileMap.size() == m_TileMapWidth);for (int i = 0; i < m_TileMapHeight; i++ ){	for (int j = 0; j < m_TileMapWidth; j++ )	{                assert(m_pTileMap[j].size() == m_TileMapHeight);		fin >> m_pTileMap[j];		}}

In debug these will tell you if the assertions aren't true.

They may indicate to you that i and j should be the other way around.

If not, remove them again.

##### Share on other sites
hey thx for the replys but i'm still stuck. I've tried to go through the for loops step by step and i think i've gone blind now i'll drop the cod i'm working on now with something more so you can get a better picture of what might be wrong

//this is the reading part// creating tile map on runtimem_pTileMap.resize(m_TileMapHeight);if (!m_pTileMap.empty()) { 	for (int j = 0; j < m_TileMapHeight; j++) 	{ 		m_pTileMap[j].resize(m_TileMapWidth); 	} } // read from file what textures belong to what tileassert(m_pTileMap.size() == m_TileMapHeight);for (int j = 0; j < m_TileMapHeight; j++){	for (int i = 0; i < m_TileMapWidth; i++)        {                assert(m_pTileMap[j].size() == m_TileMapWidth);	        fin >> m_pTileMap[j]; 	}	fin.ignore();}// and this is the using part// draw mapfor (int i = 0; i < m_TileMapHeight; i++){	for (int j = 0; j < m_TileMapWidth; j++)        {		if (FAILED(m_tiles.DrawTile(device,(float)i,(float)j, m_pTileMap[j],64.0f,64.0f,D3DCOLOR_RGBA(255,255,255,255))))		{			MessageBox(NULL, "Couldn't draw tile!!", "ERROR", MB_ICONERROR);			return E_FAIL;		}	}}

Now it's totally screwed but i get an error where i have to return a pointer to a texture and that is no good, so that's probably because i'm reading some thing that is not proper.

what i'm supposed to be reading is:
MAPWIDTH:5MAPHEIGHT:6MAP:000001001000002000001120012201