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

1

11

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.

Edelman answered 29/6, 2012 at 17:47 Comment(5)
Are you not importing gl.h ? Only because I had weird crashes before where I didn't import headers and GCC didn't moanMcintire
Do you have an current, glewInit()'d GL context by the time you get to that glGenBuffers() call?Mimamsa
Yep, I have already called glewInit(), glfwInit() and glfwOpenWindow() before calling glGenBuffers().Edelman
Are you called glewInit() after glfwOpenWindow()? Does your video card support OpenGL 1.5?Boelter
Oh, thank you, that was the problem. I was calling glewInit() before glfwOpenWindow().Edelman
M
21

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.

Marela answered 3/9, 2012 at 18:7 Comment(1)
+1 Yes. If using glfw, you need to call glewInit() after glfwMakeContextCurrent(window).Laryssa

© 2022 - 2024 — McMap. All rights reserved.