4 Replies - 2024 Views - Last Post: 03 October 2012 - 06:09 AM Rate Topic: -----

#1 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 995
  • Joined: 30-July 08

Problem with "vector iterator not dereferencable"

Posted 03 October 2012 - 03:17 AM

Hi again! I get a "weird" error (title) sometimes when I kill a enemy in my game.

It is in this function (collision detection), when I introduced my addEntity(...) on row 61.
I started to randomly get the error in the title, and I don't know why! It works sometimes and won't work others. Anyone has any idea how to do this correctly?

void EntityManager::collitionDetection()
{
	double r1 = 0, r2 = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0, xDelta = 0, yDelta = 0, d = 0;
	bool hit = false;

	if(vecEntity.size() > 1)
	{
		for(std::vector<Entity*>::iterator i = vecEntity.begin(); i != vecEntity.end(); i++)
		{
			r1 = vecEntity[0]->getRadie();
			x1 = vecEntity[0]->getX();
			y1 = vecEntity[0]->getY();

			x2 = (*i)->getX();
			y2 = (*i)->getY();
			r2 = (*i)->getRadie();

			xDelta = (x1 - x2);
			yDelta = (y1 - y2);

			if(sqrt(pow(xDelta,2) + pow(yDelta, 2)) <= (r1+r2))
			{
				if(dynamic_cast<Enemy*>(*i))
				{
					(*i)->setAlive(false);
					//addEntity(new Explosion(((*i)->getX()), (*i)->getY(), true));
					dynamic_cast<Player*>(vecEntity[0])->updateLife();
				}
			}
		}
	}

	for(std::vector<Entity*>::iterator i = vecEntity.begin(); i != vecEntity.end(); i++)
	{
		for(std::vector<Entity*>::iterator j = vecEntity.begin(); j != vecEntity.end(); j++)
		{
			if(i != j)
			{		
				r1 = (*i)->getRadie();
				x1 = (*i)->getX();
				y1 = (*i)->getY();

				r2 = (*j)->getRadie();
				x2 = (*j)->getX();
				y2 = (*j)->getY();
				xDelta = x1-x2;
				yDelta = y1-y2;

				if(sqrt(pow(xDelta,2) + pow(yDelta, 2)) <= (r1+r2))
				{
					if(dynamic_cast<Enemy*>(*j) && dynamic_cast<Bullet*>(*i))
					{
						if(dynamic_cast<Bullet*>(*i)->isFriendly())
						{
							(*j)->setAlive(false);
							(*i)->setAlive(false);
							
							if(dynamic_cast<Enemy*>(*j))
							{
								dynamic_cast<Player*>(vecEntity[0])->updateScore();
								addEntity(new Explosion(((*j)->getX()), (*j)->getY(), true));
							}
						}
						/*if(!(*i)->checkStatus())
						{
							addEntity(new Explosion(((*i)->getX()), (*i)->getY()));
						}*/
					}
					if(dynamic_cast<Player*>(*j) && (dynamic_cast<Bullet*>(*i)->isFriendly() == false))
					{
						dynamic_cast<Player*>(vecEntity[0])->updateLife();
						(*i)->setAlive(false);
					}
				}
			}
		}	
		for(std::vector<Entity*>::iterator i = vecEntity.begin(); i != vecEntity.end(); i++)
		{
			if(!(*i)->checkStatus())
			{
				delete *i;
				i = vecEntity.erase(i);
				
				return;
			}			
		}
	}
}
Here is the function addEntity
void EntityManager::addEntity(Entity *entity)
{
	vecEntity.push_back(entity);
}

This post has been edited by FrozenSnake: 03 October 2012 - 03:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with "vector iterator not dereferencable"

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2959
  • View blog
  • Posts: 10,183
  • Joined: 08-August 08

Re: Problem with "vector iterator not dereferencable"

Posted 03 October 2012 - 05:23 AM

Is it possible that one of the objects vecEntity points to might get deleted elsewhere but the vector element isn't erased?
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2959
  • View blog
  • Posts: 10,183
  • Joined: 08-August 08

Re: Problem with "vector iterator not dereferencable"

Posted 03 October 2012 - 05:32 AM

Heh, it looks like that is happening at lines 81 and 82. Try:
vecEntity.erase(i);
delete i;


Was This Post Helpful? 0
  • +
  • -

#4 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 995
  • Joined: 30-July 08

Re: Problem with "vector iterator not dereferencable"

Posted 03 October 2012 - 05:46 AM

I think no, I only have delete and erase inside there.
It delete objects outside the screen too, but it delete/erase only in the collision function.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2959
  • View blog
  • Posts: 10,183
  • Joined: 08-August 08

Re: Problem with "vector iterator not dereferencable"

Posted 03 October 2012 - 06:09 AM

Right, but you're deleting the object and then trying to erase the pointer in the vector. If you erase the vector and then delete the object you should be fine. See post #3.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1