Crash when iterating std::map

Hello.

I am iterating an std::map and get a crash between end of one and start of the next iteration. It always occurs on the same iteration, but only given my code within the iteration, so it is possible there are out of bounds pointer accesses messing with data I'm not seeing.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// ...

typedef std::list<RenderObject*> ObjectList;

template <typename T> class Scene
{
private:
	typedef MipMap<T> Tex;
	typedef std::map<std::string, Tex*, StringCompareCaseInsensitive> TexMap;
	typedef std::map<std::string, GLuint, StringCompareCaseInsensitive> GLTexMap;
	typedef std::pair<std::string, GLuint> GLTexMapPair;

public:

	// ...

	void initToGL() {
		std::cout << "Soft iteration:" << std::endl; // This iteration works fine
		for (TexMap::iterator it = textures.begin(); it != textures.end(); it++) {
			std::cout << "Texture: " << it->first << std::endl;
		}

		system("@pause>nul");

		for (TexMap::iterator it = textures.begin(); it != textures.end(); it++) { // Crash right here
			std::cout << "Making texture " << std::flush;
			std::cout << it->first << "..." << std::endl;

			MipMap<T>* mipMap = it->second;
			GLuint texture;
			glGenTextures(1, &texture);
			glBindTexture(GL_TEXTURE_2D, texture);

			std::cout << "Generated and bound GL textures." << std::endl;

			RGBTexture tex(mipMap->getWidth(0), mipMap->getHeight(0));
			T* topMap = mipMap->getMipMap(0);
			for (UINT x = 0; x < mipMap->getWidth(0); x++) {
				for (UINT y = 0; y < mipMap->getHeight(0); y++) {
					tex.setColorAt(x, y, topMap->getColorAt(x, y));
				}
			}

			std::cout << "Built RGBTexture with dimensions " << tex.getWidth() << "x" << tex.getHeight() << std::endl;

			gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, tex.getWidth(), tex.getHeight(),
				GL_RGB, GL_UNSIGNED_BYTE, (void*) tex.getRawData());
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

			std::cout << "Generated mipmaps." << std::endl;

			glBindTexture(GL_TEXTURE_2D, 0);

			std::cout << "Unbound texture." << std::endl;

			glTextureList.emplace(GLTexMapPair(it->first, texture));

			std::cout << "Emplaced texture." << std::endl;
		}
		std::cout << "Made textures." << std::endl;
		for (ObjectList::iterator it = objectList.begin(); it != objectList.end(); it++) {
			(*it)->initToGL();
		}
	}

	// ...

private:
	TexMap textures;
	GLTexMap glTextureList;
	ObjectList objectList;
	UINT frameCount;
	Camera* cam;
};


For cases where it works, I sometimes do get rendering artifacts.

e.g. http://img30.imageshack.us/img30/4610/jyp3.png

The TexMap textures is handed to the class from the outside. Maybe someone sees something that should be obvious. I am still multi-checking all my other classes but find nothing. Let me know if we need to go deeper. <_< Thanks!
When I run in Debug I get an error message box which looks like it was ripped straight out of Win95 saying

Debug Assertion Failed
[...]
File: [...] vc\include\list
Line: 1295

Expression: sequence not ordered

[...]


This happens when calling .erase() on the map with a key as parameter.

Does that count?
I changed an std::list to std::vector which fixed that problem. (for whatever reason)

The crash occured still. I was doing some low level pointer access calculation incorrectly which I noticed after checking for the dozenth time.

So yeah thanks for imposing ancient chinese sensei strictness on me rofl
Last edited on
Topic archived. No new replies allowed.