Fix native memory leak in AsAnyMarshaler.ConvertLayoutToNative on exception#126909
Open
Fix native memory leak in AsAnyMarshaler.ConvertLayoutToNative on exception#126909
Conversation
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/33ae7353-2afd-4be1-9081-898caba946da Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix native memory leak in LayoutImplementation.ConvertToNative
Fix native memory leak in AsAnyMarshaler.ConvertLayoutToNative on exception
Apr 14, 2026
jkoritzinsky
approved these changes
Apr 14, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an exception-path leak in CoreCLR’s AsAnyMarshaler.ConvertLayoutToNative (in System.Private.CoreLib) by ensuring unmanaged memory allocated via Marshal.AllocCoTaskMem is freed if LayoutTypeConvertToUnmanaged throws during layout marshaling.
Changes:
- Wrap
LayoutTypeConvertToUnmanagedintry/catchwithinConvertLayoutToNative. - Free the
CoTaskMembuffer on exception before rethrowing.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an exception-path native memory leak in AsAnyMarshaler.ConvertLayoutToNative within CoreLib interop marshaling by ensuring native allocations (and any accumulated cleanup work) are released when LayoutTypeConvertToUnmanaged throws.
Changes:
- Wraps the
LayoutTypeConvertToUnmanagedcall inConvertLayoutToNativewith atry/catch. - On exception, destroys the
cleanupWorkListand frees theMarshal.AllocCoTaskMembuffer before rethrowing.
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.
Description
AsAnyMarshaler.ConvertLayoutToNativeleaks theMarshal.AllocCoTaskMem-allocated buffer whenLayoutTypeConvertToUnmanagedthrows — the exception unwinds past thereturn pNativeHomewith no cleanup.Fix
Wrap the conversion call in a
try/catchthat frees on failure, matching the defensive pattern already used in sibling code in the same file:Changes
src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs: Addedtry/catcharoundLayoutTypeConvertToUnmanagedcall inConvertLayoutToNativeto free native memory on exception.Testing
Risk
Low — strictly adds a cleanup path that was previously missing; no behavioral change on the happy path.