Mr. Margarine
Mr. Margarine

Reputation: 31

How to fix "Access violation executing location" when using GLFW and GLAD

I caught this exception when running the program:

Exception thrown at 0x0000000000000000 in OpenGL project.exe: 0xC0000005: Access violation executing location 0x0000000000000000.

This is my code below:

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

int main()
{
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window{ glfwCreateWindow(800, 800, "Win", NULL, NULL) };

    if (window == NULL)
    {
        std::cout << "Doesn't work";
        glfwTerminate();
        return -1;
    }

    gladLoadGL();
    glfwMakeContextCurrent(window);

    glViewport(0, 0, 800, 800);

    glClearColor(50.0f, 1.3f, 1.7f, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);

    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

The code stopped working when I tried to add color

This is the link to the tutorial I was using: https://youtu.be/z03LXhRBLGI?list=PLPaoO-vpZnumdcb4tZc4x5Q-v7CkrQ6M-

I have no idea what the exception could be referring to

I tried moving a few things around like putting this block into the while statement

    glViewport(0, 0, 800, 800);

    glClearColor(50.0f, 1.3f, 1.7f, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);

Upvotes: 1

Views: 86

Answers (1)

Rabbid76
Rabbid76

Reputation: 210978

The OpenGL Context is required and must be the current Context to load the OpenGL API. Therefore glfwMakeContextCurrent must be called before gladLoadGL (see also glad):

glfwMakeContextCurrent(window);
gladLoadGL();

Upvotes: 1

Related Questions