1

I have a project in OpenGL and I'm trying to load shaders. I use GLuint shader=glCreateShader(shaderType); for that. The problem is, when it tries to run this line I get EXC_BAD_ACCESS (code=1, address=0x0) error (in XCode).

I found some answers that I might not have initialized glfw, or glew. But it seems it works just fine. This is my initialization code:

if (!glfwInit()) {
        fprintf(stderr, "Couldn't initialize GLFW.\n");
        exit(EXIT_FAILURE);
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwSetErrorCallback(errorCallback);
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL Test", nullptr, nullptr);

    if (!window) {
        fprintf(stderr, "Couldn't create window.\n");
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    if (glewInit() != GLEW_OK) {
        fprintf(stderr, (char*)"Couldn't initialize GLEW library.\n");
        exit(EXIT_FAILURE);
    }

    initOpenGLProgram(window);

in initOpenGLProgram():

glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
glfwSetKeyCallback(window, keyCallback);

shaderProgram = new ShaderProgram((char*)"shaders/vshader.glsl", NULL, (char*)"shaders/fshader.glsl");

in new ShaderProgram:

printf("Loading vertex shader...\n");
vertexShader=loadShader(GL_VERTEX_SHADER,vertexShaderFile);

and in loadShader method (this line throws an error):

GLuint shader=glCreateShader(shaderType);

Also I have a question. I need to do my project in OpenGL 3.3, but when i check version with glGetString(GL_VERSION) I get 4.1 version. Will there be now problem?

Tomasz Kasperek
  • 1,147
  • 3
  • 13
  • 35
  • This is slightly confusing. Could you edit your question please? (I'm not entirely sure what all causes the error.) –  Jul 15 '16 at 21:41
  • Calling glCreateShader function causes the error. – Tomasz Kasperek Jul 15 '16 at 21:46
  • I think you normally need a `glewExperimental = TRUE` before calling `glewInit()`. See http://stackoverflow.com/questions/8302625/segmentation-fault-at-glgenvertexarrays-1-vao. – Reto Koradi Jul 16 '16 at 02:52
  • @RetoKoradi I also did it with `glewExperimental = true` and it changes nothing – Tomasz Kasperek Jul 16 '16 at 08:28
  • @RetoKoradi: Actually OP doesn't need GLEW at all. In MacOS X you either get all the functions you need through the OpenGL framework, or not. There's an extension mechanism, but that' really just for extensions. Core features are tied to the OpenGL version supported by the OS version and the framework version it offers. – datenwolf Jul 16 '16 at 12:34
  • @datenwolf I hadn't noticed the platform reference, but I now see that XCode is mentioned, so I guess that gives it away. Yes, definitely no need for GLEW on macOS. – Reto Koradi Jul 16 '16 at 16:03

2 Answers2

0

The error is one of memory. Check the return value of glCreateShader().This says, "A null pointer is returned if there are no tokens left to retrieve.." This says:

  • The pointer could have never been initialized.
  • The pointer could have been accidentally written over because you overstepped the bounds of an array.
  • The pointer could be part of an object that was casted incorrectly, and then written to.
  • Any of the above could have corrupted a different pointer that now points at or near this pointer, and using that one corrupts this one (and so on).
Community
  • 1
  • 1
0

I found a problem. In my case it was a bad project configuration in XCode. I had 2 libGlew dylibs linked.

libraries

When I removed libGLEWmx.1.13.0.dylib everything started working.

Tomasz Kasperek
  • 1,147
  • 3
  • 13
  • 35
  • 1
    In MacOS X you should not require GLEW at all. I strongly suggest you remove GLEW, except if you require extensions *specific to Apple*. – datenwolf Jul 16 '16 at 12:35
  • The problem is it is a university project, and we have to use it. If I did my own project I would probably use Apple extensions – Tomasz Kasperek Jul 16 '16 at 12:44
  • 1
    I think you misunderstood me. In MacOS X you don't have to use extensions at all. You don't need GLEW in MacOS X to use things from modern OpenGL, *unless* you want to use some extensions. So your best course of action is to drop GLEW from your project; or in case you're cross platform omit it from MacOS X builds. – datenwolf Jul 16 '16 at 12:57