Debug assertion fail; line 256

Hi,

I'm a beginner c++ programmer and I tried to make a game, but there's one error that keeps popping up.
Debug Assertion Failed! <...> line 256.

I think that the problem is in this part of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Level::update()
{
	// here is where we'll be able to deal with fireball moving
	// and extra stuff
		for (Iter = npc.begin(); Iter != npc.end(); Iter++)
		{	
			(*Iter)->idleUpdate();
			if ((*Iter)->isAlive() == false)
			{
				Sprite *temp = *Iter;
				Iter--;
				delete temp;
				npc.remove(temp);
			}

		}
}


Could you check for anything I might have done wrong here?
Thanks in advance :)
P. S. Iter is an iterator for a list of one of the classes' (Sprite) pointers. The list itself is called npc.
Last edited on
If Iter is pointing to npc.begin(), wouldn't Iter-- be problem?
I just tried to update the code to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Level::update()
{
	bool t = false;
	// here is we'll be able to deal with fireball moving
	// and extra stuff
		for (Iter = npc.begin(); Iter != npc.end(); Iter++)
		{	
			(*Iter)->idleUpdate();
			if (t)
				Iter--;
			t = false;
			if ((*Iter)->isAlive() == false)
			{
				Sprite *temp = *Iter;
				if (Iter != npc.begin())
				{
					Iter--;
					t = true;
				}
				delete temp;
				npc.remove(temp);
			}
		}
}


That took care of the error, but the game itself just crashes. If there are no other mistakes here, I guess there must be a mistake somewhere else. Thanks anyway :)
If Iter points to npc.begin() you do the following:
1
2
3
4
Sprite * temp = *Iter; //OK
delete temp; //OK
npc.remove(temp); //OK but...
Iter++; //since you removed temp Iter points nowhere and thus cannot be incremented. 


I think you should use list::erase()
The problem is at the beginning. If it is at the beginning you remove the 'Sprite' and thus invalidate 'Iter'.

Better would be something like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Level::update()
{
	// here is we'll be able to deal with fireball moving
	// and extra stuff
		Iter = npc.begin();
		while(Iter != npc.end())
		{	
			(*Iter)->idleUpdate();
			if ((*Iter)->isAlive())
				++Iter;
			else
			{
				Sprite *temp = *Iter;
				Iter = npc.erase(Iter);
				delete temp; // Delete the object after you erased it from the container otherwise you'd have an invalid object in you containe for a short while
			}
		}
}
The last code worked like a charm :) Thanks a lot :)
Perhaps you could also explain to a beginer, what's the difference between erase and remove?
what's the difference between erase and remove?


The STL containers erase/remove methods remove elements. But if you use the STL algorithm remove, remove_if, I don't think they do removal.What the function return is a "marker" where everything before "marker" is retain and all else after are elements to be removed but they are not removed yet. You need one more step to call STL containers erase/remove methods to do actual removal.
what's the difference between erase and remove?


erase() removes the object pointed to by the iterator and returns the next valid iterator.
remove() removes all objects which equals to the object provided. It does not return anything

See
http://www.cplusplus.com/reference/stl/list/remove/
http://www.cplusplus.com/reference/stl/list/erase/
Thanks a lot :)
Topic archived. No new replies allowed.