Access Violation on STL Vector.clear()

Hey all,

I'm using a C++ vector for storing C++ strings, unfortunately when I delete the strings after use I get an access violation error.

The code for using the vector is very simple so it won't be a huge paste.

In my header file I have:

vector<string> fileNames;

In my cpp file I have:
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
	/* Sky Box */
	fileNames.push_back("Textures/skybox/lostatseanight_front.jpg");
	fileNames.push_back("Textures/skybox/lostatseanight_back.jpg");
	fileNames.push_back("Textures/skybox/lostatseanight_left.jpg");
	fileNames.push_back("Textures/skybox/lostatseanight_right.jpg");
	fileNames.push_back("Textures/skybox/lostatseanight_top.jpg");
	fileNames.push_back("Textures/skybox/lostatseanight_bottom.jpg");

        //Using SOIL to load textures in my program
	for (int i = 0; i < 6; i++)
	{
		skyBox[i] = SOIL_load_OGL_texture
			(
			fileNames[i].c_str(),
			SOIL_LOAD_AUTO,
			SOIL_CREATE_NEW_ID,
			SOIL_FLAG_INVERT_Y
			);

		if(skyBox[i] == 0)
			return false;

		// Typical Texture Generation Using Data From The Bitmap
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
	}

	fileNames.clear();


The access violation happens when clear is called, and unfortunately I simply have no clew why.. I have used vectors for a long time and this has never happened before.

Help appreciated!
Thanks.
It seems that theres an issue with me using vectors in my project altogether.

I got rid of all the code you see above, no pushing data no clearing.. the only thing left was the vector<string> fileNames; in my class header, when the program exits I still get the access violation.
You have undefined behavior somewhere. Use the debugger and valgrind to find out where the problem is. Enable debug iterators as well.
Last edited on
Problem solved, it was a silly typo in my skyBox[] variable.

I had;
1
2
	GLuint skyBox[5];
	vector<string> fileNames;


The skybox was supposed to be [6].

The for loop iterated 6 times, therefore writing data outside the bounds of the skybox[] array, which sequentially lead to writing that data into my vector.

Thanks for the tips btw, i've never heared of these debugging tools and features, they look very interesting!
Topic archived. No new replies allowed.