SDL and OpenGL extensions causing a segfault

I’m writing an OpenGL application using SDL as my windowing library, but I’m getting a crash (process terminated with status 3) when I call glGenBuffers. I’ve used gl* calls before this point with no problem, but as soon as i use glGenBuffers, I crash. I’ve tried searching for the problem on Google, and nearest I can tell, it’s causing a segfault somewhere, but I really don’t know where it’s happening.

Here are my #includes,

#define GLEW_STATIC
#define NO_SDL_GLEXT
#include "gl/glew.h"
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>


#include <stdint.h>
#include <cmath>

Here’s main(),


int main(int argc, char* argv[])
{
   Application* app = new Application(640, 480);
   OpenGL* gl = new OpenGL(app);


   while (app->InputLoop())
   {
      gl->Render();
   }


   delete app;
   return 0;
}

Here’s the constructor for my Application class,


Application::Application(int width, int height)
:  screen(NULL),
   running(false)
{
   /* Initialize SDL. */
   if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
      return;


   /* Set SDL_GL attributes. */
   SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   8);
   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  8);
   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
   SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);


   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
   SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);


   /* Create our screen. */
   screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL);
   if (screen == NULL)
      return;


   SDL_WM_SetCaption("OpenGL Test", NULL);


   running = true;
   return;
}

and here’s the constructor for my OpenGL class:


OpenGL::OpenGL(Application* App)
:  app(App),
   width(0),
   height(0)
{
   /* If this check returns false, it means that the OpenGL class was created before the Application,
      or after the main loop has already ended. */
   if (app->isRunning() == false)
      return;


   /* Grab the screen dimensions. */
   width = app->getWidth();
   height = app->getHeight();


   GLenum err = glewInit();
   if (err != GLEW_OK)
   {
      printf("! ERROR: '%s'
", glewGetErrorString(err));
      return;
   }


   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   glClearDepth(1.0f);
   glViewport(0, 0, width, height);


   float vertex[3] = {0.0f, 0.0f, 0.0f};
   GLuint vbo = 0;
   glGenBuffers(1, &vbo); //[b] <- this causes a crash![/b]


   //glBindBuffer(GL_ARRAY_BUFFER, vbo);
   //glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
   //glEnableVertexAttribArray(0);


   //glBindBuffer(GL_ARRAY_BUFFER, vbo);


   //glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);


   return;
}

When I comment out glGenBuffers, the application runs fine, I get a black window as expected. When it’s uncommented, I get a white window for about 1/10 of a second, it closes, and I get a status of 3. glGenBuffers is the first function I use which comes from an extension, so I investigated GLEW to see if the problem lied there. I tried grabbing the function pointer myself with glext, but the exact same crash happened, in the exact same spot. My libraries are “-lmingw32 -lSDLmain -lSDL -lopengl32 -lglu32”, with glew.c simply added to the project and linked statically.

My IDE is Code::Blocks on Windows 7 64-bit, using GNU GCC Compiler, with an updated version of MinGW. I use the -g and -Wall flags, but the only warnings I get are the hundreds from glew.c that I always get from glew.c - The only warnings from my code are about unused variables.

Call to SDL_SetVideoMode should be:


/* Set SDL_GL attributes. */
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

screen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL);

Well I’ll be damned, that fixed it! Thank you so much!