Repository navigation
[rcore][win32] Stop SetWindowIcon() from leaking the previous icon - #6239
Merged
Merged
Conversation
Owner
|
@Ne0nWinds nice fix! thanks! |
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.
Any icon created with
CreateIconIndirect()must be deleted withDestroyIcon()when it is no longer in use; otherwise, it causes a memory leak. The current win32 backend doesn't do this, so every call toSetWindowIconleaks a small amount of memory.Testing
Windows has a function that returns the total number of GDI and USER objects that are currently allocated. This function can be used to easily detect memory leaks.
Simple Win32 memory leak test:
If you run the above code on current
master, you'll see the number of win32 objects grow without bound. Every timeSetWindowIconis called, it allocates 3 GDI objects and 1 USER object. These objects don't get freed at any point.Solution
The icon can be safely deleted when there are no longer any references to it.
SetWindowIconassigns two references to the same icon:ICON_SMALLandICON_BIG.Calling
SendMessagewith theWM_SETICONevent returns the icon previously assigned to the window, which we can use to destroy it. We do this only after updating bothICON_SMALLandICON_BIGto use the new icon.When
CloseWindowis called, the current icon handle is saved withWM_GETICON. The icon gets destroyed after the window gets destroyed, ensuring the window doesn't hold a bad reference to the icon.Relevant Win32 docs: