Jump to content
  • Advertisement
Sign in to follow this  
thallish

memory leak with STL container (vector)?

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by thallish
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 ;) )

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 runtime
m_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 tile
assert(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 map
for (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:
5
MAPHEIGHT:
6
MAP:
00000
10010
00002
00000
11200
12201

Share this post


Link to post
Share on other sites
If your memory allocation is sufficiently deterministic, the damaged address will be the same each time. Just put a data breakpoint at the address that's reported as damaged, and you'll be able to see where things are going bad.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!