Repository navigation
[rcore][desktop] Fix clipboard image memory leak - #5968
Merged
Merged
Conversation
Contributor
Author
|
Since fileData is now released with SDL_free(), would you prefer removing this comment as well? Image GetClipboardImage(void)
//...
size_t dataSize = 0;
void *fileData = NULL;
for (int i = 0; i < SDL_arraysize(imageFormats); i++)
{
// NOTE: This pointer should be free with SDL_free() at some point <------this
fileData = SDL_GetClipboardData(imageFormats[i], &dataSize);
if (fileData)
{
image = LoadImageFromMemory(imageExtensions[i], fileData, (int)dataSize);
SDL_free(fileData);
if (IsImageValid(image))
{
TRACELOG(LOG_INFO, "Clipboard: Got image from clipboard successfully: %s", imageExtensions[i]);
return image;
}
}
}
//...
return image;It seems the comment is no longer needed after this change, but I wanted to check your preference. |
Owner
|
@LuisR385 Yeah, you can remove the comment |
Contributor
Author
aight! |
Owner
|
@LuisR385 thanks for the review |
This was referenced Aug 25, 2026
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.
PR Title
Fix memory leak in
GetClipboardImage()Summary
This PR fixes a memory leak in
GetClipboardImage().SDL_GetClipboardData()returns a heap-allocated buffer that must be released withSDL_free(). The current implementation loads the image from the returned buffer but never frees it, resulting in a memory leak.This PR simply calls
SDL_free(fileData)immediately afterLoadImageFromMemory().The goal of this PR is to keep the change as small as possible and consistent with raylib's design philosophy.
Environment
Reproduction
The following sample reproduces the issue.
Reproduction Images
Example 1
Copy this image:
https://images.unsplash.com/photo-1657632843433-e6a8b7451ac6?fm=jpg&q=60&w=3000&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTd8fDRrJUU1JUEzJTgxJUU3JUI0JTk5fGVufDB8fDB8fHww
CRT reports:
Example 2
Copy the raylib logo:
https://upload.wikimedia.org/wikipedia/commons/f/f4/Raylib_logo.png
CRT reports:
The leaked allocation size roughly matches the clipboard image data size.
Fix
The only change is adding
after
LoadImageFromMemory(...)This ensures the temporary clipboard buffer returned by SDL is released after the image has been decoded.
Notes
I intentionally kept this PR as small as possible to match raylib's design philosophy.
I noticed there are a few other places where additional validation or guards could potentially be added, but those are outside the scope of this fix, so I did not include them in this PR.
I'd appreciate your opinion: