Debug assertion fail; line 256

Jan 16, 2012 at 12:20pm
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 :)
Jan 16, 2012 at 12:23pm
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 Jan 16, 2012 at 12:23pm
Jan 16, 2012 at 1:03pm
If Iter is pointing to npc.begin(), wouldn't Iter-- be problem?
Jan 16, 2012 at 1:24pm
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 :)
Jan 16, 2012 at 1:44pm
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()
Jan 16, 2012 at 3:17pm
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
			}
		}
}
Jan 17, 2012 at 7:02am
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?
Jan 17, 2012 at 8:09am
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.
Jan 17, 2012 at 8:38am
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/
Jan 17, 2012 at 12:25pm
Thanks a lot :)
Topic archived. No new replies allowed.