<p>While <a href="https://www.khronos.org/opengl/wiki/Debug_Output" rel="noreferrer">Debug Output</a> is good, and manual <code>glGetError</code> usage is adequate, it's often better to employ a more <a href="https://www.khronos.org/opengl/wiki/Debugging_Tools" rel="noreferrer">dedicated tool for finding exactly where OpenGL errors came from</a>.</p>

<p><a href="https://renderdoc.org/" rel="noreferrer">RenderDoc</a> is probably the most up-to-date tool for this process, but there are quite a few in various states of functionality. These tools can also give you a detailed log of every OpenGL call you've made.</p>

CC BY-SA 3.0
2017-12-03T15:39:50.970Z
2017-12-03T15:39:50.970Z
Nicol Bolas
<p>If you can't go to OpenGL 4.3 (or your implementation doesn't support <code>glDebugMessageCallback</code>s), you can simplify your code in a number of ways.</p>

<p>The first is to move the calls to <code>glGetError()</code> into a function like this:</p>

<pre><code>void checkGLError()
{
    GLenum err;
    while((err = glGetError()) != GL_NO_ERROR){
        std::cout &lt;&lt; err;
    }  
}
</code></pre>

<p>That reduces your code to:</p>

<pre><code>unsigned int buffer = 0;
glGenBuffers(1, &amp;buffer);
checkGLError();
glBindBuffer(GL_ARRAY_BUFFER, buffer);
checkGLError();
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), vertices, GL_STATIC_DRAW); 
checkGLError();
</code></pre>

<p>However, calling <code>glGetError()</code> frequently can hurt performance, so it's best to conditionalize it so it only gets called in debug builds by doing something like this:</p>

<pre><code>#if DEBUG
#define checkGLError() debugCheckGLError()
#else
#define checkGLError()
#endif
</code></pre>

<p>Then name the actual function <code>debugCheckGLError()</code>. It will get called in debug builds but not release builds.</p>

CC BY-SA 3.0
2017-12-04T16:23:33.083Z
I used the debug macro with the other other answer. Thanks.
user8277998
CC BY-SA 3.0
2017-12-06T02:47:15.983Z
You can go even further with the macro and do something like this: https://github.com/bkaradzic/bgfx/blob/8d471959eb3cbc16c0e7fdac25efcf842abd2ad1/src/renderer_gl.h#L994 so your code just becomes: `GL_CHECK(glGenBuffers(1, &buffer));` and reports everything you need.
Julien Guertault
CC BY-SA 4.0
2020-03-04T05:49:07.690Z
I don't understand `while( !(err = glGetError())`  Why the `!` ?  Shouldn't it be: `while(err = glGetError())` so that it loops and outputs error messages?  Otherwise, isn't it an infinite loop that repeatedly displays `0` on the first successful call?
Wyck
CC BY-SA 4.0
2020-03-07T21:07:03.590Z
@Wyck Yes, you are correct. I've updated it to make it more explicit by comparing it to the proper error value.
user1118321
CC BY-SA 4.0
2017-12-03T07:43:27.347Z
2020-03-07T21:06:14.593Z
2020-03-07T21:06:14.593Z
user1118321
<p>Yes, there is a better way!  OpenGL 4.3 and later support the <a href="http://docs.gl/gl4/glDebugMessageCallback" rel="noreferrer"><code>glDebugMessageCallback</code></a> API, which allows you to specify a function in your app that GL will call to issue a warning or error. In this function you can do whatever you like, such as setting a breakpoint in the debugger, or printing the error to a log file. This way you only need to setup the callback during initialization—much nicer than putting <code>glGetError</code> calls everywhere.</p>

<p>To turn on this functionality your GL context also needs to be a "debug context", which is specified by setting a flag when you create the context. The details will depend on which OS / windowing system / framework you're using. A debug context might be slower (on the CPU) than a regular context, but that's the price you pay for more detailed debugging info, kinda like turning optimizations off in the compiler.</p>

<p>See also <a href="https://www.khronos.org/opengl/wiki/Debug_Output" rel="noreferrer">this wiki page</a> for more information. There's a bunch of other debugging-related functionality. For instance you can give human-readable names to textures, buffers, framebuffers, etc using <a href="http://docs.gl/gl4/glObjectLabel" rel="noreferrer"><code>glObjectLabel</code></a>, which will then (I assume) be used in error messages. There are also APIs for turning off certain messages or categories of messages, in case you get spammed by warnings that you don't care about or some such.</p>

CC BY-SA 4.0
2021-07-13T10:44:17.713Z
With the [ARB_debug_output](https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_debug_output.txt) extension it is possible to use `glDebugMessageCallback` even with OpenGL 4.0.
Wendelin
CC BY-SA 3.0
2017-12-02T23:17:43.107Z
2017-12-02T23:17:43.107Z
Nathan Reed
<p>Whenever I make semantic or syntax errors in OpenGL, either I get a black screen or the program crashes. I looked up on internet on how to do error handling in OpenGL and in the documentations I found <code>glGetError()</code>. To my understanding, <code>glGetError()</code> will return one or more error code(s) if I call it after calling a "normal" OpenGL function, provided I have made some error. Also I have to call it in while loop to get all the errors. </p>

<p>I have multiple problems with this. One I can't find the line number on which the error has occurred, second it doesn't tell me properly why the error occurred (not a big problem though) and lastly I have to include a while loop after each OpenGL function call. </p>

<p>Here's an example, </p>

<pre><code>unsigned int buffer = 0;
glGenBuffers(1, &amp;buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), vertices, GL_STATIC_DRAW); 
</code></pre>

<p>Now if I write it with <code>glGetError()</code>, it would look something like</p>

<pre><code>unsigned int err = 0;
glGenBuffers(1, &amp;buffer);
while( !(err = glGetError()) ){
    std::cout &lt;&lt; err;
}  
glBindBuffer(GL_ARRAY_BUFFER, buffer);
while( !(err = glGetError()) ){
    std::cout &lt;&lt; err;
}  
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), vertices, GL_STATIC_DRAW); 
while( !(err = glGetError()) ){
    std::cout &lt;&lt; err;
}  
</code></pre>

<p>Surely this isn't the way to go, right ? The source code looks so ugly.  </p>

<p>How do I do proper error handling with OpenGL ?</p>

<p>My graphics card supports OpenGL 4.4 and I am using C++ . </p>

CC BY-SA 3.0
2017-12-06T02:45:01.223Z
I partially answered to a similar question recently. See https://computergraphics.stackexchange.com/a/5773/182
Julien Guertault
CC BY-SA 4.0
2020-02-06T13:54:58.117Z
shouldn't that read `while ( err = glGetError() ) )`
johannes_lalala
CC BY-SA 4.0
2020-02-21T17:42:15.887Z
It returns `GL_NO_ERROR` which I assumed to have value 0, when there is no error, that's why I had `!=` there. In any case   it should be `while((err = glGetError()) != GL_NO_ERROR)`.
user8277998
CC BY-SA 4.0
2021-07-10T08:43:39.747Z
I like the way The Cherno wrote his error handling code and he goes through it really clearly. https://www.youtube.com/watch?v=FBbPWSOQ0-w&list=PLlrATfBNZ98foTJPJ_Ev03o2oq3-GGOS2&index=10 . I will however be using glDebugMessageCallback once I can figure it out:)
Sam Keightley
<opengl><debugging>
How to do error handling with OpenGL?
user8277998
# How to do error handling with OpenGL?

(Asked by: username_0 on 2017-12-02)

Whenever I make semantic or syntax errors in OpenGL, either I get a black screen or the program crashes. I looked up on internet on how to do error handling in OpenGL and in the documentations I found `glGetError()`. To my understanding, `glGetError()` will return one or more error code(s) if I call it after calling a "normal" OpenGL function, provided I have made some error. Also I have to call it in while loop to get all the errors.

I have multiple problems with this. One I can't find the line number on which the error has occurred, second it doesn't tell me properly why the error occurred (not a big problem though) and lastly I have to include a while loop after each OpenGL function call.

Here's an example,

```
unsigned int buffer = 0;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), vertices, GL_STATIC_DRAW); 

```

Now if I write it with `glGetError()`, it would look something like

```
unsigned int err = 0;
glGenBuffers(1, &buffer);
while( !(err = glGetError()) ){
    std::cout << err;
}  
glBindBuffer(GL_ARRAY_BUFFER, buffer);
while( !(err = glGetError()) ){
    std::cout << err;
}  
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), vertices, GL_STATIC_DRAW); 
while( !(err = glGetError()) ){
    std::cout << err;
}  

```

Surely this isn't the way to go, right ? The source code looks so ugly.

How do I do proper error handling with OpenGL ?

My graphics card supports OpenGL 4.4 and I am using C++ .

**Tags:** opengl, debugging

> **Comments:**
> **username_1**: I partially answered to a similar question recently. See https://computergraphics.stackexchange.com/a/5773/182  
> **username_2**: shouldn't that read `while ( err = glGetError() ) )`  
> **username_0**: It returns `GL_NO_ERROR` which I assumed to have value 0, when there is no error, that's why I had `!=` there. In any case   it should be `while((err = glGetError()) != GL_NO_ERROR)`.  
> **username_3**: I like the way The Cherno wrote his error handling code and he goes through it really clearly. https://www.youtube.com/watch?v=FBbPWSOQ0-w&list=PLlrATfBNZ98foTJPJ_Ev03o2oq3-GGOS2&index=10 . I will however be using glDebugMessageCallback once I can figure it out:)  

---

## 3 answers

### Answer by username_7 on 2017-12-02. Score: 14 | Accepted answer

Yes, there is a better way! OpenGL 4.3 and later support the [`glDebugMessageCallback`](http://docs.gl/gl4/glDebugMessageCallback) API, which allows you to specify a function in your app that GL will call to issue a warning or error. In this function you can do whatever you like, such as setting a breakpoint in the debugger, or printing the error to a log file. This way you only need to setup the callback during initialization—much nicer than putting `glGetError` calls everywhere.

To turn on this functionality your GL context also needs to be a "debug context", which is specified by setting a flag when you create the context. The details will depend on which OS / windowing system / framework you're using. A debug context might be slower (on the CPU) than a regular context, but that's the price you pay for more detailed debugging info, kinda like turning optimizations off in the compiler.

See also [this wiki page](https://www.khronos.org/opengl/wiki/Debug_Output) for more information. There's a bunch of other debugging-related functionality. For instance you can give human-readable names to textures, buffers, framebuffers, etc using [`glObjectLabel`](http://docs.gl/gl4/glObjectLabel), which will then (I assume) be used in error messages. There are also APIs for turning off certain messages or categories of messages, in case you get spammed by warnings that you don't care about or some such.

> **Comments:**
> **username_8**: With the [ARB_debug_output](https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_debug_output.txt) extension it is possible to use `glDebugMessageCallback` even with OpenGL 4.0.  

---

### Answer by username_4 on 2017-12-03. Score: 9

While [Debug Output](https://www.khronos.org/opengl/wiki/Debug_Output) is good, and manual `glGetError` usage is adequate, it's often better to employ a more [dedicated tool for finding exactly where OpenGL errors came from](https://www.khronos.org/opengl/wiki/Debugging_Tools).

[RenderDoc](https://renderdoc.org/) is probably the most up-to-date tool for this process, but there are quite a few in various states of functionality. These tools can also give you a detailed log of every OpenGL call you've made.


---

### Answer by username_5 on 2017-12-03. Score: 1

If you can't go to OpenGL 4.3 (or your implementation doesn't support `glDebugMessageCallback`s), you can simplify your code in a number of ways.

The first is to move the calls to `glGetError()` into a function like this:

```
void checkGLError()
{
    GLenum err;
    while((err = glGetError()) != GL_NO_ERROR){
        std::cout << err;
    }  
}

```

That reduces your code to:

```
unsigned int buffer = 0;
glGenBuffers(1, &buffer);
checkGLError();
glBindBuffer(GL_ARRAY_BUFFER, buffer);
checkGLError();
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), vertices, GL_STATIC_DRAW); 
checkGLError();

```

However, calling `glGetError()` frequently can hurt performance, so it's best to conditionalize it so it only gets called in debug builds by doing something like this:

```
#if DEBUG
#define checkGLError() debugCheckGLError()
#else
#define checkGLError()
#endif

```

Then name the actual function `debugCheckGLError()`. It will get called in debug builds but not release builds.

> **Comments:**
> **username_0**: I used the debug macro with the other other answer. Thanks.  
> **username_1**: You can go even further with the macro and do something like this: https://github.com/bkaradzic/bgfx/blob/8d471959eb3cbc16c0e7fdac25efcf842abd2ad1/src/renderer_gl.h#L994 so your code just becomes: `GL_CHECK(glGenBuffers(1, &buffer));` and reports everything you need.  
> **username_6**: I don't understand `while( !(err = glGetError())`  Why the `!` ?  Shouldn't it be: `while(err = glGetError())` so that it loops and outputs error messages?  Otherwise, isn't it an infinite loop that repeatedly displays `0` on the first successful call?  
> **username_5**: @username_6 Yes, you are correct. I've updated it to make it more explicit by comparing it to the proper error value.  

---

