Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glDebugMessageCallback causes segfault

I'm in a bit of a catch-22 here. I can't debug my C++ OpenGL program because activating debug messages causes a segfault.

I have a debug callback function I register:

static void APIENTRY openglCallbackFunction(
  GLenum source,
  GLenum type,
  GLuint id,
  GLenum severity,
  GLsizei length,
  const GLchar* message,
  const void* userParam) {
  (void)source; (void)type; (void)id;
  (void)severity; (void)length; (void)userParam;
  fprintf(stderr, "%s\n", message);
  if (severity==GL_DEBUG_SEVERITY_HIGH) {
    fprintf(stderr, "Aborting...\n");
    abort();
  }
}

And I initiate the debug context in the following code:

this->window = glfwCreateWindow(this->winx, this->winy, "Cortex Stretcher", NULL, NULL);
  if(this->window == NULL) {
    fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
    glfwTerminate();
    return -1;
  }
  glfwMakeContextCurrent(this->window);

  GLint flags; glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
  if (flags & GL_CONTEXT_FLAG_DEBUG_BIT)
  {
     cout << "Debug output enabled!" << endl;
     glEnable(GL_DEBUG_OUTPUT);
     glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
     glDebugMessageCallback(openglCallbackFunction, nullptr);
    //  glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
  }

If I merely comment out glDebugMessageCallback(openglCallbackFunction, nullptr); then my code runs without any errors (aside from my model rendering incorrectly which is why I'm trying to debug).

But if I try to register the callback, then my code segfaults (at registration). Any idea why? This is essentially just copy-pasted code.

Note: glGetString(GL_VERSION) returns 4.5.0 NVIDIA 375.39

like image 668
Carson McNeil Avatar asked Aug 08 '17 00:08

Carson McNeil


1 Answers

What kind of GL loading mechanism are you using? Your code does create a context and makes it current, but you never load any GL function pointers. The typical gl.h header will only contain the GL functions up to GL 1.1, and those are also the only ones you can rely to be exported by your OpenGL library in a platform-independent manner. The fact that your compiler (and linker) does not complain about glDebugMessageCallback indicates that you use some GL loader like glew, glad, or whatever. These typically work by declaring a function pointer for every GL function, which are initalized to NULL, and will be loaded after you call some initialization function. Since you do no such thing before trying to set the debug callback, you are just calling a NULL pointer.

like image 140
derhass Avatar answered Sep 23 '22 06:09

derhass