Repository navigation
Optimize OnDrawItem to prevent GDI resource exhaustion by caching scaled icon size - #14123
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #14123 +/- ##
===================================================
+ Coverage 76.01271% 77.15542% +1.14271%
===================================================
Files 3279 3279
Lines 645325 645339 +14
Branches 47720 47721 +1
===================================================
+ Hits 490529 497914 +7385
+ Misses 145491 143737 -1754
+ Partials 9305 3688 -5617
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a critical GDI resource exhaustion issue that causes crashes in owner-drawn controls during high-frequency drawing operations. The fix introduces intelligent caching of scaled icon widths to prevent repeated GDI object creation in hot code paths.
Key Changes:
- Implemented a dictionary-based cache using (Cursor, DPI) tuples to store computed icon widths
- Refactored
OnDrawItemto use the cached values instead of repeatedly creating Icon objects - Added proper cache cleanup in the
End()method to prevent memory leaks
SimonZhao888
approved these changes
Dec 10, 2025
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #14122
Root Cause
The exception:
An object could not be created, possibly due to a lack of memory, but most likely due to invalid inputwas triggered because of how
OnDrawItemhandled icon creation in a high-frequency drawing path:Repeated GDI object creation in hot path
Each call to OnDrawItem executed:
This pattern allocates multiple unmanaged GDI resources per draw cycle. Under heavy redraw (scrolling, hover, DPI changes), these allocations accumulate quickly, exhausting GDI handles.
Proposed changes
Customer Impact
Regression?
Risk
Screenshots
Before
When using a PropertyGrid to edit properties of type Cursor, resizing the Cursor UITypeEditor drop-down window causes the application to stop responding or crash.
ResizeCursorEdiorIssue.mp4
After
AfterChanges.mp4
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow