skip to Main Content

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;
}

2

Answers


  1. You need to initialize the GL entry points (function pointers) after setting the active context with something like:

    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
        // failed to load...
    

    Also, prevent system GL headers being included if using the GLAD library:

    #include <glad/glad.h>
    ...
    #define GLFW_INCLUDE_NONE
    #include <GLFW/glfw3.h>
    ...
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search