Deleting elements form STL list while iterating over it

Im trying to remove elements from STL list, but im getting a weird runtime error: "Exception thrown at 0x00CC417B in PhysicsEngine.exe: 0xC0000005: Access violation reading location 0x0000000C" I dont know what to do to fix this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void QuadTree::insert(Object *col) {
	if (nodes[0] != NULL) {
		int index = getIndex(col);

		if (index != -1) {
			(*nodes[index]).insert(col);
			return;
		}
	}

	objects.push_back(col);
	col->setPartition(this);

	if (objects.size() > MAX_OBJECTS && level < MAX_LEVELS) {
		if (nodes[0] == NULL) {
			split();
		}

		std::list<Object*>::const_iterator it = objects.begin(); 
		while(it != objects.end()) {
			int index = getIndex(*it);

			if (index != -1) {
				(*nodes[index]).insert(*it);
				it = objects.erase(it);
			}
			else{
				++it;
			}
		}
	}
}
How do you know that all Object* in the objects are valid? Someone could call the insert() with a nullptr.
The getIndex() probably expects a valid pointer too.

How do you know that the value returned by getIndex() is either -1 or a valid index to the 'nodes'?


In other words: you have to debug a bit deeper to find out which statement crashes. The erase in itself looks ok.
OK i found the problem. The problem is related to something else, and i thought it would raise problems, but i dont know how to fix it.
I have made a topic about it http://www.cplusplus.com/forum/general/195882/ the issue lies within the merge function.
Topic archived. No new replies allowed.