7 Replies - 1274 Views - Last Post: 27 June 2011 - 02:15 PM Rate Topic: -----

#1 atwal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-February 11

Error in using vector stl library in game programming, vector iteratr

Posted 27 June 2011 - 04:14 AM

Hello friends i am making a very simple game. Here in this project some balls sprites are moving. I am using Vector stl to store the Sprite pointer. When i debug it there is no problem and balls are moving but when i close the application it give a error Vector Iteration not decerementable. after that when i click on ignore button it shows libraries out of range &&0
Please could any one can help me to solve it.
You can download source code and debugged project from this link.
i am using visual studio 2010 to crate the project.

http://code-info.com...d/TestFore2.zip

thanks

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Error in using vector stl library in game programming, vector iteratr

#2 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,326
  • Joined: 14-February 08

Re: Error in using vector stl library in game programming, vector iteratr

Posted 27 June 2011 - 05:32 AM

Here's your problem.
// Delete and remove the sprites in the sprite vector
vector<Sprite*>::iterator siSprite;
for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)
{
   delete (*siSprite);
   m_vSprites.erase(siSprite);
   siSprite--;
 }



Why are you incrementing the iterator and then decrementing it at the end :s

You can just use the Clear() function to clear the vector and call all of the elements destructors.

m_vSprites.Clear();


Was This Post Helpful? 0
  • +
  • -

#3 aaron1178   User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 170
  • View blog
  • Posts: 1,311
  • Joined: 22-October 08

Re: Error in using vector stl library in game programming, vector iteratr

Posted 27 June 2011 - 05:45 AM

See everyone, stayscrisp is legend... He helps those that cannot be answered by the average bear... You're our yogi bear stayscrisp :D
Was This Post Helpful? 0
  • +
  • -

#4 hype261   User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 29
  • Joined: 02-March 11

Re: Error in using vector stl library in game programming, vector iteratr

Posted 27 June 2011 - 06:40 AM

View Poststayscrisp, on 27 June 2011 - 05:32 AM, said:

Here's your problem.
// Delete and remove the sprites in the sprite vector
vector<Sprite*>::iterator siSprite;
for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)
{
   delete (*siSprite);
   m_vSprites.erase(siSprite);
   siSprite--;
 }



Why are you incrementing the iterator and then decrementing it at the end :s

You can just use the Clear() function to clear the vector and call all of the elements destructors.

m_vSprites.Clear();




Calling Clear by itself won't be enough. Because the Sprites are allocated on the heap this will lead to a memory leak. I am surprised the original code ran in debug mode. Calling erase on a vector invalidates that iterator and any iterator aftwards. This means that the decrement operator isn't going to work. Here is what I would do...

vector<Sprite*>::iterator siSprite;
for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); ++siSprite)
{
   delete (*siSprite);
}
m_vSprites.clear();


This post has been edited by hype261: 27 June 2011 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,326
  • Joined: 14-February 08

Re: Error in using vector stl library in game programming, vector iteratr

Posted 27 June 2011 - 07:19 AM

Good point hype261, that is actually what I meant. Should have been more explicit :) Although I always feel that there must be a better way considering iterating and then calling clear will most likely iterate through the list again.

While we are on the subject though atwal you might want to look into the boost library and use something such as a scoped_ptr or shared_ptr. It will help to dodge possible future headaches and memory leaks.
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,326
  • Joined: 14-February 08

Re: Error in using vector stl library in game programming, vector iteratr

Posted 27 June 2011 - 08:10 AM

View Postaaron1178, on 27 June 2011 - 01:45 PM, said:

See everyone, stayscrisp is legend... He helps those that cannot be answered by the average bear... You're our yogi bear stayscrisp :D


Lol! Glad to help :D
Was This Post Helpful? 0
  • +
  • -

#7 atwal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-February 11

Re: Error in using vector stl library in game programming, vector iteratr

Posted 27 June 2011 - 12:05 PM

Thanks every one I changed my code and now its working. The code i was using is a old style and not valid anymore thats why i was getting problem. now i changed it like this
void GameEngine::CleanupSprites()
{
  // Delete and remove the sprites in the sprite vector
	vector<Sprite*>::iterator siSprite = m_vSprites.begin();
  while(siSprite != m_vSprites.end())
  {
    delete *(siSprite++);   
  }

   m_vSprites.clear();
}


Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,326
  • Joined: 14-February 08

Re: Error in using vector stl library in game programming, vector iteratr

Posted 27 June 2011 - 02:15 PM

How about this

template<typename T>
void SafeDelete(T& t) 
{ 
	while(!t.empty()) 
	{
		delete t.back();
		t.pop_back(); 
	}
}



I like templates :)

This post has been edited by stayscrisp: 27 June 2011 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1