c++macosopenglglfwglad

EXC_BAD_ACCESS from glClear(GL_COLOR_BUFFER_BIT)?


I've looked around and apparently I've got the choice between this solution, and my problem was the same as the following: text, but I still don't know how to solve this problem. When I use glClear(GL_COLOR_BUFFER_BIT); in xcode, it will alarm that Thread 1: EXC_BAD_ACCESS (code=1, address=0x0), but when I annotate it with "//", the project will work.enter image description here Thanks in advance!

I try to "//" this code, but it will not clear the window' render.

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Solution

  • I could repeat the issue with your code. I also checked the suggested solution. you should call gladLoadGLLoader after glfwMakeContextCurrent.

    #include <iostream>
    #include <glad/glad.h>
    #include <GLFW/glfw3.h>
    int main(void)
    {
        GLFWwindow* window;
    
        /* Initialize the library */
        if (!glfwInit())
            return -1;
    
        /* Create a windowed mode window and its OpenGL context */
        window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
        if (!window)
        {
            glfwTerminate();
            return -1;
        }
    
        /* Make the window's context current */
        glfwMakeContextCurrent(window);
    
    
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
            // error out
        }
    
        /* Loop until the user closes the window */
        while (!glfwWindowShouldClose(window))
        {        
            /* Render here */
            glClear(GL_COLOR_BUFFER_BIT);
    
            /* Swap front and back buffers */
            glfwSwapBuffers(window);
    
            /* Poll for and process events */
            glfwPollEvents();
        }
    
        glfwTerminate();
        return 0;
    }
    

    Also it was not a problem with glClear. Any OpenGL call would also cause crash if GL function pointers are not properly initialized.