glGenBuffers() crashing with Segmentation fault. (C++/GLFW/GLEW)

structinf picture structinf · Jun 29, 2012 · Viewed 10.4k times · Source

So, in my project I am using a seperate class to create buffers called Buffer.cpp. Here is the constructor

#define GLEW_STATIC
#define GLEW_NO_GLU
#define GLFW_NO_GLU

#include "GL/glew.h"
#include "GL/glfw.h"

Buffer::Buffer(GLenum _type, const void *data, GLsizei _size, GLenum usage) :  type(_type), size(_size)
{
  ...

  //Generate Buffer
  glGenBuffers(1, &buffer);

  ...
}

And the definitions of the members:

GLuint buffer;
const GLsizei size; 
const GLenum type;
Buffer(GLenum, const void*, GLsizei, GLenum);

The problem is that when I try to generate a buffer using for example this command:

Buffer vBuffer(GL_ARRAY_BUFFER, vertexPositions, sizeof(vertexPositions), GL_STATIC_DRAW);

the program crashes at glGenBuffers() with termination status "-1073741819". I tried debugging the program and this is what I got:

Program received signal SIGSEGV, Segmentation fault.

My card supports OpenGL 1.5 so that's not the case.

It is also worth to note that I compiled a static glew library myself.

EDIT: I finally fixed the problem. The problem was that I was calling glewInit() before creating an OpenGL rendering context.

Answer

Mārtiņš Možeiko picture Mārtiņš Možeiko · Sep 3, 2012

So your problem was that you were creating OpenGL context after you called glewInit() - and thus glewInit() had no way to set up GL entry points properly.

In this case glewInit() probably did return error code. Are you verifying error codes from functions? It should return GLEW_OK.