[C++] Memory violation with glGenBuffers

Hello,

I’m using OpenGL with C++ through glew and freeglut. I also do successfully load the necessary OpenGL-extentions.
I get an memory violation error


Unhandled exception at 0x74DBC9F5 in MyProgram.exe: 0xC0000005: Access violation executing location 0x00000000.

when trying to call this:

glGenBuffers(1, &vboHandle);

VboHandle is a GLuint member of my Vao-class. I think the problem is that it’s a GLuint and not a GLuint* but why?

Thanks!

To me this sounds more like GLEW is not correctly initialized and therefore glGenBuffers is not a valid function pointer, did you read this wiki page, in particular the part about using glewExperimental? Are you initializing GLEW after creating an OpenGL context? - it needs one to obtain the function pointers.

glewInit() ist returning GLEW_OK … so I think that’s not the problem.

EDIT: I think I found the problem: I statically initalized the object calling glGenBuffers. At this point the OpenGL isn’t initialized context yet.
EDIT 2: Got the program up and running. Thanks anyway!

Okay nothing is fine :frowning:

I get memory violations at the more recent OpenGL functions starting at glDrawElements. glslDevil crashes the program as soon as it calls glGenVertexArray. Strangly some functions seem to work: I can compile shaders and get errorlogs. Despite glslDevil crashing vbo,ibo and vao handles get set.
But the fact remains that glewInit() returns GLEW_OK. glewExperimental is set to true. Can I specifically check wheather the modern OpenGL function pointers are loaded?

Thanks!

There’s nothing recent about glDrawElements(); that was introduced in OpenGL 1.1.

Just treat the pointers as boolean values, e.g. “if (glGenVertexArray) …”.

Function Pointer are loaded.
It crashes with

Unhandled exception at 0x5936FD84 (ig7icd32.dll) in CPHemeral.exe: 0xC0000005: Access violation reading location 0x00000000.

I’m at a loss here.

If you’re loading GLEW correctly but you still get memory violations at glDrawElements a possible cause might be that you have a vertex attrib array enabled but no vertex attrib pointer set for it. Possibly the old GL_INDEX_ARRAY common mistake, even?

Either way, I’d definitely recommend running your program under a good debugger which can help you determine where the violations are occurring and what might be causing them.

Here is the interception log:

glViewport(0,0,480,320)
glClearColor(0.000000,0.000000,0.000000,1.000000)
glClear(GL_COLOR_BUFFER_BIT)
glGenVertexArrays(1,03E92FE8)
glBindVertexArray(1)
glGenBuffers(1,03E92FEC)
glBindBuffer(GL_ARRAY_BUFFER,1)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0,4,GL_FLOAT,false,0,00000000)
glDisableVertexAttribArray(0)
glGenBuffers(1,03E92FF0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,2)
glBindVertexArray(0)
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0)
glBindVertexArray(1)
glCreateShader(GL_VERTEX_SHADER)=1 
glShaderSource(1,1,0030F408,00000000)
glCompileShader(1)
glGetShaderiv(1,GL_COMPILE_STATUS,0030F3DC)
glGetShaderiv(1,GL_INFO_LOG_LENGTH,0030F3D0)
glGetShaderInfoLog(1,12,00000000,03D6F800)
glCreateShader(GL_FRAGMENT_SHADER)=2 
glShaderSource(2,1,0030F3FC,00000000)
glCompileShader(2)
glGetShaderiv(2,GL_COMPILE_STATUS,0030F3DC)
glGetShaderiv(2,GL_INFO_LOG_LENGTH,0030F3D0)
glGetShaderInfoLog(2,12,00000000,03D6F840)
glCreateProgram()=3 
glAttachShader(3,1)
glAttachShader(3,2)
glBindAttribLocation(3,0,"position")
glLinkProgram(3)
glGetProgramiv(3,GL_LINK_STATUS,0030F3F8)
glGetProgramiv(3,GL_INFO_LOG_LENGTH,0030F3EC)
glGetProgramInfoLog(3,12,00000000,03D6F900)
glValidateProgram(3)
glBindBuffer(GL_ARRAY_BUFFER,1)
glBufferData(GL_ARRAY_BUFFER,48,0307D6D8,GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,2)
glBufferData(GL_ELEMENT_ARRAY_BUFFER,6,0055C040,GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0)
glUseProgram(3)
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_SHORT,00000000) GLSL=3 
----->glClearColor(0.000000,0.000000,0.000000,1.000000)
----->glClear(GL_COLOR_BUFFER_BIT)
----->glBindVertexArray(1)
----->glUseProgram(3)
----->glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_SHORT,00000000)

Is something wrong with that? If not I’ll give you the code I’m using to setup and draw.

I had a glGenBuffers access violation and it took me days to figure out that my whole issue was I needed to call glewInit() after I created the window. I’m using sdl to handle windows for me since it’s easy and has everything I need but it should be the same regardless of what lib you use to create the window.

SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_OPENGL);
glewInit();

Thats not the problem. Using freeGlut and calling glewInit after the contex and window are created.

Look at your glDrawElements calls - what have you bound to GL_ELEMENT_ARRAY_BUFFER when you make each call?

Also, this:

glBindBuffer(GL_ARRAY_BUFFER,1)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0,4,GL_FLOAT,false,0,00000000)
glDisableVertexAttribArray(0)

Means that in your VAO you first enable attrib array 0, then you set it’s pointer, then you disable the attrib array. So the final state of your VAO has attrib array 0 disabled. It also has 0 bound to GL_ARRAY_BUFFER (not important as it’s the binding at the time that glVertexAttribPointer is made that matters) and 0 bound to GL_ELEMENT_ARRAY_ARRAY_BUFFER.

Also, this: […]

Thats a mistake I made while trying to fix my problems.

Here are some important functions I use:


void Vao::initGL() {
	if(vaoHandle == 0) glGenVertexArrays(1, &vaoHandle);
	glBindVertexArray(vaoHandle);
	bindVboGL();
	bindIboGL();
	glBindVertexArray(0);
	
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

void Vao::bindVboGL() {
	glGenBuffers(1, &vboHandle);
	glBindBuffer(GL_ARRAY_BUFFER, vboHandle);
	for(std::vector<VertexAttribute*>::size_type i = 0; i < attribs.size(); i++) {
		attribs.at(i).bindVertexPointerGL();
	}
}

void Vao::bindIboGL() {
	glGenBuffers(1, &iboHandle);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboHandle);
}

void Vao::drawGL() {
	if(vaoHandle == 0) initGL();
	glBindVertexArray(vaoHandle);
	for(std::vector<VaoEntry*>::size_type i = 0; i < entries.size(); i++) {
		if(!entries.at(i)->isVisible()) continue; 
		checkShaderProgramGL(entries.at(i)->getShaderId());
		checkEntryUpdatesGL();
		entries.at(i)->getShaderProgram()->bindGL();
		// entries.at(i)->getShaderProgram()->uploadUniformsGL(entries.at(i)->getUniformKey());
		glDrawElements(mode, entries.at(i)->getCount(), GL_UNSIGNED_SHORT, 
			(GLvoid* ) (entries.at(i)->getOffset() * sizeof(GLshort)));
		entries.at(i)->getShaderProgram()->unbindGL();
	}
	glBindVertexArray(0);
}

void VertexAttribute::bindVertexPointerGL() {
	glEnableVertexAttribArray(index);
	glVertexAttribPointer(index, size, type, normalized, stride, (GLvoid*) (index * sizeof(type)));
}

GL_ELEMENT_ARRAY_BUFFER should be 0 right before glDrawElements.
Thanks for you help so far!