glad causes glfwSwapBuffers to return error message



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

int main()

    GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);

    while (!glfwWindowShouldClose(window))

    return 0;

Background information
My Operating System is the latest version of windows 10
I use visual studio v16.6.3 (latest)
I am very new (just learned of openGL today) and am trying to learn how to make graphics with openGL
I use glad and GLFW

The program is supposed to spawn a blank unresponsive window
Except when running the program, the window gets created but then the glClear command returns error "Exception thrown at 0x0000000000000000 in Project1.exe: 0xC0000005: Access violation executing location 0x0000000000000000."

What I have tried
(Unsuprisingly) commenting out the problem code caused the program to run correctly
I have reinstalled my graphics card drivers
I have tried installing a bunch of different glad combinations on the generator site
I have tried running the program in 32bit and 64bit (using the cooresponding GLFW bit-type)
Commenting out #include <glad.h> and <glad.c> caused the program to function properly

Program Properties:
. C/C++:
. . General:
. Linker:
. . General:
. . Input:
. Glad:

asked on Stack Overflow Jul 6, 2020 by RoBoCoden

1 Answer


The behavior is totally correct. So what GLAD does is having a #define glFoo glad_glFoo for every GL function Foo in the <glad.h>, with glad_glFoo being a function pointer to the appropriate type. A;ll of these function pointers are initialized to NULL.

So when you call glClear(GL_COLOR_BUFFER_BIT) you actually call glad_glClear(GL_COLOR_BUFFER_BIT) which of course ends up as calling address 0:

"Exception thrown at 0x0000000000000000 in Project1.exe: 0xC0000005: Access violation executing location 0x0000000000000000."

Before you can call a GLAD function pointer, you must to all of these things:

  • have successfully called gladLoadGL while you had a GL context active for the calling thread,
  • have the same GL context bound to the thread you call your GL function on (or a compatible one, but that is platform-specific)
  • make sure that the GL function actually is provided by the implementation, for example
    1. by checking for a specific GL version which guarantees the presence of that function 1.b) by requesting a specific required GL version at context creation time and bailing out if it is not met
    2. by checking for the presence of GL extensions which mandate the availability of that function
    3. with GLAD, you could even check for NULL pointers, as by the way GLAD works, it is guaranteed that a non-NULL function pointer implies the presence of the function since GLAD internally basically does 1. and 2. before querying a function pointer. Note that is not true for the underlying GL extension mechanism. You might query a an unsupported function and the implementation still might supply a non-NULL function pointer as a result. Calling that is undefined behavior it the implementation has not advertised the presence of this function via the GL version or extension string(s).

Since glClear is present in every GL version since the beginning, all your code is lacking is a gladLoadGL.

answered on Stack Overflow Jul 6, 2020 by derhass • edited May 8, 2021 by derhass

User contributions licensed under CC BY-SA 3.0