glDebugMessageCallback causes segfault
Asked Answered
P

1

7

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

Palatine answered 8/8, 2017 at 0:53 Comment(2)
glDebugMessageCallback is only available in OpenGL > 4.3. Your error message makes me think that you are either using OpenGL 3.3 or 2.1, which both don't support that.Quirk
That is not the case. If I print out glGetString(GL_VERSION); I get 4.5.0 NVIDIA 375.39. I'm using a Titan X.Palatine
L
10

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.

Leiva answered 13/8, 2017 at 11:9 Comment(2)
Ah, I am indeed using glew. I understand what it's doing now, but I didn't at the time I posted this question, and you're totally right, my debug setup is called before I initialize glew! Thank you!Palatine
I had shamefully sandwiched my debugging setup between GLFW and gl3w initialization steps. This solved my problem.Odyl

© 2022 - 2024 — McMap. All rights reserved.