Segmentation fault on glClearBuffer*

I’m trying to go through the OpenGL SuperBible 6th ed., but was having a bit of trouble using the provided sb6.h framework for setting up windows and whatnot. I have done some legacy OpenGL using GLFW for windowing, so I figured I could just wrap the code in GLFW rather than what the book provides.

However, I am segfaulting whenever I reach glClearBufferfv:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <stdlib.h>
#include <stdio.h>

static void errorCallback(int error, const char *description)
{
  fputs(description, stderr);
}

static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
    glfwSetWindowShouldClose(window, GL_TRUE);
  }
}

static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };

int main(void)
{
  glewExperimental = true;
  glewInit();

  GLFWwindow *window;

  glfwSetErrorCallback(errorCallback);

  if (!glfwInit()) {
    return EXIT_FAILURE;
  }

  window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  if (!window) {
    glfwTerminate();
    return EXIT_FAILURE;
  }

  glfwMakeContextCurrent(window);

  glfwSetKeyCallback(window, keyCallback);

  int width, height;
  glfwGetFramebufferSize(window, &width, &height);
  glViewport(0, 0, width, height);

  int maxBuffers = 0;
  glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxBuffers);

  printf("GL_MAX_DRAW_BUFFERS = %d
", maxBuffers);

  while (!glfwWindowShouldClose(window)) {
    glClearBufferfv(GL_COLOR, (GLint) 0, red);

    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  glfwDestroyWindow(window);

  glfwTerminate();

  return EXIT_SUCCESS;
}

According to my glewinfo, my hardware supports up to GL_VERSION_4_0, but I’m guessing I’m missing something else. According to the OpenGL reference, glGetError() should be flagged, but when I set up a sigaction to caught the segfault, I got GL_NO_ERROR. Any help would be appreciated.

You are initializing GLEW before creating an OpenGL context (AFAIK GLFW does that as part of glfwCreateWindow). Since GLEW needs to query the OpenGL context for (extension) functions you must initialize GLEW after creating the OpenGL context and making it current.

That was it. Thank you!