Free leaked allocations from render objects when using rlgl with OpenGL 3.3 #1302
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related issues
This may be related to #1301, but this PR addresses problems found in 3.1, unlike the issue, which appears to be centered around 3.0.
Summary
Two leaks come from any
RenderBatch
objects and defaultShader
resources that are created.Default RenderBatch
The allocations occur here:
raylib/src/rlgl.h
Lines 4088 to 4094 in 00fda3b
The default RenderBatch created by
rlglInit()
(and seemingly any other RenderBatch) does not record the number of buffers that it has created, which prevents its buffers (and the memory allocated for them) from getting cleaned up when it's passed toUnloadRenderBatch
.This is resolved by adding the assignment to the
buffersCount
which is evaluated by theUnloadRenderBatch
function during clean-up.Default Shader
The allocation occurs here on rlgl.h(3937).
raylib/src/rlgl.h
Lines 3934 to 3938 in 00fda3b
The
locs
is cleaned up for normal shaders...raylib/src/rlgl.h
Lines 3100 to 3110 in 00fda3b
...but since the default shader goes through a special path, this gets missed.
raylib/src/rlgl.h
Lines 4062 to 4073 in 00fda3b
Adding an
RL_FREE
to handle the allocation stored inlocs
from the default shader for its clean-up routine appears to handle things.Quick note: I'm not a regular contributor to Raylib, so any guidance on whether the clean-up logic for the default shader should be consolidated into the normal free function for shaders would be appreciated.
Other Leaks - GLFW?
Visual Studio reports that two additional leaks remain, but appears to be attributed to GLFW: one leak stemming from the creation of the window on Windows while the other is from creating some buffer for retrieving joysticks.
Raylib's logic for initializing and de-initializing GLFW seems fine on the surface, so I thought it'd be out of scope.
System Environment
Here's a print-out of my environment, which reflects that I'm working with OpenGL 3.3.
Test Code
Since the issue seemed localized to OpenGL, it seemed reasonable to simply start and stop
an OpenGL context in order to reproduce the problem. To that endeavor, only
InitWindow
and
CloseWindow
are called.The leak was identified using the CRT Debug Library available in MSVC as well as the Visual Studio 2019 Memory Usage profiler.