From 90befff4b8ee15d52d8497957af4acff76fd5410 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 16 Sep 2020 13:17:16 +0200 Subject: [PATCH] Make sure to detach data before deleting Before deleting certain objects, they must be detached from their parents. That's the case for shader objects after linkage to shader program and also for the texture/cubemaps/renderbuffers attached to framebuffers. If objects are deleted before detached, they are kept in memory to avoid accessing deleted data. --- src/rlgl.h | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/rlgl.h b/src/rlgl.h index 287dc7b3..09d74736 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -1456,11 +1456,23 @@ void rlDeleteTextures(unsigned int id) void rlDeleteRenderTextures(RenderTexture2D target) { #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(SUPPORT_RENDER_TEXTURES_HINT) - if (target.texture.id > 0) glDeleteTextures(1, &target.texture.id); + if (target.texture.id > 0) + { + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); // Detach texture from FBO + glDeleteTextures(1, &target.texture.id); + } if (target.depth.id > 0) { - if (target.depthTexture) glDeleteTextures(1, &target.depth.id); - else glDeleteRenderbuffers(1, &target.depth.id); + if (target.depthTexture) + { + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0); + glDeleteTextures(1, &target.depth.id); + } + else + { + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0); + glDeleteRenderbuffers(1, &target.depth.id); + } } if (target.id > 0) glDeleteFramebuffers(1, &target.id); @@ -1483,8 +1495,12 @@ void rlDeleteVertexArrays(unsigned int id) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) if (RLGL.ExtSupported.vao) { - if (id != 0) glDeleteVertexArrays(1, &id); - TRACELOG(LOG_INFO, "VAO: [ID %i] Unloaded vertex data from VRAM (GPU)", id); + if (id != 0) + { + glBindVertexArray(0); + glDeleteVertexArrays(1, &id); + TRACELOG(LOG_INFO, "VAO: [ID %i] Unloaded vertex data from VRAM (GPU)", id); + } } #endif } @@ -3094,8 +3110,18 @@ Shader LoadShaderCode(const char *vsCode, const char *fsCode) { shader.id = LoadShaderProgram(vertexShaderId, fragmentShaderId); - if (vertexShaderId != RLGL.State.defaultVShaderId) glDeleteShader(vertexShaderId); - if (fragmentShaderId != RLGL.State.defaultFShaderId) glDeleteShader(fragmentShaderId); + if (vertexShaderId != RLGL.State.defaultVShaderId) + { + // Detach shader before deletion to make sure memory is freed + glDetachShader(shader.id, vertexShaderId); + glDeleteShader(vertexShaderId); + } + if (fragmentShaderId != RLGL.State.defaultFShaderId) + { + // Detach shader before deletion to make sure memory is freed + glDetachShader(shader.id, fragmentShaderId); + glDeleteShader(fragmentShaderId); + } if (shader.id == 0) {