glDrawArrays access violation, multiple objects

Hi,
I’ve made Phong shaders and they worked well on the 3 first objects. When I try to add one more, the first 3 appear messed up completely. When I try to load another one, the program crashes with access violation on the command:
glDrawArrays( GL_TRIANGLES, 0, pvertices->size());
Ideas anyone?
Print screen is attached

Thanks,
Anna

One single line of code is insufficient information to adequately debug this issue.

Are you using VBOs? In that case glDrawArrays shouldn’t fail with an access violation (as the data should already be uploaded by the driver), so check whether pvertices is an invalid pointer (NULL, uninitialized, points to data thats already deleted etc.).

Apart from that idea Alfonse is right, we would need more input.

Thanks for the quick answer.
The vertices array is not NULL.
Here’s about the rest of the code:

I have a renderer class. In it’s constructor the members
GLuint vao;
GLuint buffer[3];
GLuint tex;
are initialized:

glGenVertexArrays(1 ,&vao);
glBindVertexArray( vao );

glGenBuffers(3, buffer);
glBindBuffer( GL_ARRAY_BUFFER, buffer[0]);//vertices

glBindBuffer( GL_ARRAY_BUFFER, buffer[1]);//normals

glBindBuffer( GL_ARRAY_BUFFER, buffer[2]);//tex coords

glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

There’s an “init” method which runs 1 time before all the application for compiling the shaders, making a program, linking and using the program.
Each render of all objects call in a loop a method of the renderer which handles a single object. The code of this method is mainly:

glBindBuffer( GL_ARRAY_BUFFER, buffer[0]);
glBufferData( GL_ARRAY_BUFFER, pvertices->size()*sizeof(vec4), &((*pvertices)[0]),GL_STREAM_DRAW);
glBindBuffer( GL_ARRAY_BUFFER, buffer[1]);
glBufferData( GL_ARRAY_BUFFER, pveretexNormals->size()*sizeof(vec4), &((*pveretexNormals)[0]), GL_STREAM_DRAW);

glBindBuffer( GL_ARRAY_BUFFER, buffer[2]);
glBufferData( GL_ARRAY_BUFFER, texCoords->size()*sizeof(vec2), &((*texCoords)[0]), GL_STATIC_DRAW);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imWidth, imHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, textureIm);
glGenerateMipmap(GL_TEXTURE_2D);

//Then uniforms are passed (…)

glEnable(GL_TEXTURE_2D);
glUniform1i(texMap_buffLoc,0);//this one of the uniforms fir texture 0

Here is the call for glDrawArrays

glEnable(GL_VERTEX_ARRAY);

glDrawArrays( GL_TRIANGLES, 0, pvertices->size());

glDisable(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);

Thanks again

I was told to write the models to the graphics card just once:
loading each shape to a differnt buffer object and not using the same one in a loop.
I did it and there hasn’t been access violation since that change.

I understand why it is faster. I don’t know why theres no access violation. Now I use more memory of the GPU…

As I understood every time you attach new information to a buffer object the old one is deleted. So I still don’t get why there was an error before.

Ideas anyone?

It looks like you aren’t setting up your VAO properly.
There is an example here
http://www.opengl.org/wiki/Tutorial1:Re…B/_freeGLUT%29

Also, this line is invalid
glEnable(GL_VERTEX_ARRAY)

also,
http://www.opengl.org/wiki/Common_Mistakes#glGetError