donut icon indicating copy to clipboard operation
donut copied to clipboard

GL Context is double-freed

Open rndtrash opened this issue 3 years ago • 0 comments

Problem: donut always exits ungracefully.

+---------------------------------------------------+
|    _____        /--------------------------\      |
|   /     \      |                            |     |
| \/\/     |    /     donut game               \    |
|  |  (o)(o)    |                              |    |
|  C   .---_)   \_   _________________________/     |
|   | |.___|      | /                               |
|   |  \__/      <_/                                |
|   /_____\                                         |
|  /_____/ \                                        |
| /         \                                       |
+---------------------------------------------------+

hellooooooooooo new york!!!!
Initializing SDL...
SDL Version/Compiled 2.0.12
SDL Version/Linked 2.0.12
OpenGL version loaded: 4.6
Vendor: NVIDIA Corporation
Renderer: GeForce GTX 1050 Ti/PCIe/SSE2
Version: 4.6.0 NVIDIA 460.73.01
// some unrelated messages //
X Error of failed request:  GLXBadContext
  Major opcode of failed request:  151 (GLX)
  Minor opcode of failed request:  4 (X_GLXDestroyContext)
  Serial number of failed request:  3409
  Current serial number in output stream:  3409
AL lib: (EE) alc_cleanup: 1 device not closed

Theory: this error is caused by double-free of GL context. Proof of concept:

#include <GL/glew.h>
#include <SDL2/SDL.h>

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_VIDEO); /* Initialises Video Subsystem in SDL */

    /* Setting up OpenGL version and profile details for context creation */
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
    
    /* A 800x600 window. Pretty! */
    SDL_Window* window = SDL_CreateWindow
        (
        "SDL Context",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        800, 600,
        SDL_WINDOW_OPENGL
        );
    
    /* Creating OpenGL Context */
    SDL_GLContext gl_context = SDL_GL_CreateContext(window);

    /* Loading Extensions */
    glewExperimental = GL_TRUE;
    glewInit();

    /* The following code is for error checking. 
    *  If OpenGL has initialised properly, this should print 1.
    *  Remove it in production code.
    */
    GLuint vertex_buffer;
    glGenBuffers(1, &vertex_buffer);
    printf("%u\n", vertex_buffer);
    /* Error checking ends here */

    /* Main Loop */
    SDL_Event window_event;
    while(1) {
        if (SDL_PollEvent(&window_event)) {
            if (window_event.type == SDL_QUIT) {
                /* If user is exiting the application */
                break;
            }
        }
        /* Swap the front and back buffer for flicker-free rendering */
        SDL_GL_SwapWindow(window);
    }
    
    /* Freeing Memory */
    glDeleteBuffers(1, &vertex_buffer);
    SDL_GL_DeleteContext(gl_context);
    SDL_GL_DeleteContext(gl_context); // <=
    SDL_Quit();

    return 0;
}
X Error of failed request:  GLXBadContext
  Major opcode of failed request:  151 (GLX)
  Minor opcode of failed request:  4 (X_GLXDestroyContext)
  Serial number of failed request:  299
  Current serial number in output stream:  299

rndtrash avatar May 10 '21 12:05 rndtrash