[raudio] Fix memory leak when loading .wav files - #5963
Merged
Conversation
Owner
|
@LuisR385 good catch! Thanks for the review! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Title
Fix memory leak in
UnloadMusicStream()for WAV files on MSVC Debug buildsSummary
This PR fixes a memory leak that occurs when unloading a WAV music stream using
UnloadMusicStream().On Windows 11 with MSVC (Visual Studio 2022, x64 Debug), the Microsoft CRT Debug Heap reports a remaining allocation after the application exits, even though
UnloadMusicStream()has been called.The leak is caused by the decoder context (
Music.ctxData) not being fully released.Environment
Reproduction
Actual Result
The Microsoft CRT Debug Heap reports the following memory leak:
Expected Result
UnloadMusicStream()should release all memory allocated byLoadMusicStream().No memory leaks should be reported after the application exits.
Investigation
The leaked allocation appears to originate from the internal decoder context stored in
Music.ctxData.Manually releasing this pointer removes the leak:
RL_FREE(music.ctxData);However,
ctxDatais an internal implementation detail and should not need to be released by user code.Its lifetime should be managed entirely by raylib.
Fix
Release the decoder-specific context (
ctxData) duringUnloadMusicStream()so that all resources allocated byLoadMusicStream()are properly cleaned up.This keeps ownership of internal resources inside raylib and eliminates the reported memory leak.
Verification
Tested with:
_CrtSetDbgFlag)After this change, no memory leaks are reported.