Commit 329f2eb99edc16e85dd592d8278ed198916f0ffe

Cameron Gutman 2018-10-31T20:17:53

Fix crash when GL_LoadFunctions()/GLES2_LoadFunctions() fails https://bugzilla.libsdl.org/show_bug.cgi?id=4350 We can't safely call GL_DestroyRenderer() until GL_LoadFunctions() succeeds because we will be missing functions that we try to use when activating the renderer for destruction if we have an GL context.

diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index f3e8326..09db12d 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -420,7 +420,7 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
 
     data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
     if (!data) {
-        GL_DestroyRenderer(renderer);
+        SDL_free(renderer);
         SDL_OutOfMemory();
         goto error;
     }
@@ -455,16 +455,21 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
 
     data->context = SDL_GL_CreateContext(window);
     if (!data->context) {
-        GL_DestroyRenderer(renderer);
+        SDL_free(renderer);
+        SDL_free(data);
         goto error;
     }
     if (SDL_GL_MakeCurrent(window, data->context) < 0) {
-        GL_DestroyRenderer(renderer);
+        SDL_GL_DeleteContext(data->context);
+        SDL_free(renderer);
+        SDL_free(data);
         goto error;
     }
 
     if (GL_LoadFunctions(data) < 0) {
-        GL_DestroyRenderer(renderer);
+        SDL_GL_DeleteContext(data->context);
+        SDL_free(renderer);
+        SDL_free(data);
         goto error;
     }
 
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index fe51b9a..f21a957 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -2122,7 +2122,7 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
 
     data = (GLES2_DriverContext *)SDL_calloc(1, sizeof(GLES2_DriverContext));
     if (!data) {
-        GLES2_DestroyRenderer(renderer);
+        SDL_free(renderer);
         SDL_OutOfMemory();
         goto error;
     }
@@ -2134,16 +2134,21 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
     /* Create an OpenGL ES 2.0 context */
     data->context = SDL_GL_CreateContext(window);
     if (!data->context) {
-        GLES2_DestroyRenderer(renderer);
+        SDL_free(renderer);
+        SDL_free(data);
         goto error;
     }
     if (SDL_GL_MakeCurrent(window, data->context) < 0) {
-        GLES2_DestroyRenderer(renderer);
+        SDL_GL_DeleteContext(data->context);
+        SDL_free(renderer);
+        SDL_free(data);
         goto error;
     }
 
     if (GLES2_LoadFunctions(data) < 0) {
-        GLES2_DestroyRenderer(renderer);
+        SDL_GL_DeleteContext(data->context);
+        SDL_free(renderer);
+        SDL_free(data);
         goto error;
     }