Repository navigation
Fix issue 15213: Bitmap finalized during Image.Save(Stream, ImageFormat) leaks native GDI+ memory - #15254
Conversation
…at) leaks native GDI+ memory
There was a problem hiding this comment.
🟡 Changes recommended
The regression test baseline can be contaminated by pending finalizers from earlier tests.
1 open finding
What changed in this PR
Prevents premature Image finalization during native GDI+ stream saves.
Changes:
- Keeps
IImagealive throughGdipSaveImageToStream. - Adds native-memory regression coverage.
| File | Description |
|---|---|
CoreImageExtensions.cs |
Extends image lifetime through native save. |
ImageTests.cs |
Tests memory growth across collections. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15254 +/- ##
=============================================
Coverage 37.84861% 37.84861%
=============================================
Files 246 246
Lines 9789 9789
Branches 1031 1031
=============================================
Hits 3705 3705
Misses 5920 5920
Partials 164 164
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
|
Once this lands, it would be great to have it backported to |
KlausLoeffelmann
left a comment
There was a problem hiding this comment.
Looks good.
Since this is leaking and a burden for Customers - let's please backport this to .NET 11 next service and .NET 10 next service.
…at) leaks native GDI+ memory (dotnet#15254) * Fix issue 15213: Bitmap finalized during Image.Save(Stream, ImageFormat) leaks native GDI+ memory * Add test case * Handle feedback --------- Co-authored-by: Simon Zhao (BEYONDSOFT CONSULTING INC) <[email protected]>
|
/backport to release/10.0 |
|
Started backporting to |

Fixes #15213
Root Cause
When
Image.Save(Stream, ImageFormat)executes the internal stream-saving path, theImageobject is not guaranteed to remain alive during the call to the nativeGdipSaveImageToStreamfunction. Under specific timing conditions, the JIT compiler or Garbage Collector (GC) may trigger finalization of theImageobject prematurely, creating a race condition with the native save operation; this results in the failure to release—or the loss of—underlying GDI+ image resources, manifesting as a native memory leak (where significant memory remains unrecovered).Proposed changes
Within
CoreImageExtensions.Save(this IImage image, Stream stream, ...)ofSystem.Private.Windows.GdiPlus, wrapGdipSaveImageToStreamin a try/finally block and callGC.KeepAlive(image)in the finally block.Customer Impact
The correction avoids abnormal native memory growth in
Save(Stream, ImageFormat)under high-pressure/specific GC timings, reducing memory risks and potential stability issues in long-duration or batch image processing scenarios.The impact on existing behavioral compatibility is minimal (only extending object survival to the end of the method), and should not change functional results or introduce visible behavioral differences.
Regression?
Risk
Screenshots
Before
After
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow