Fix marshalling generators using 0 as cleanup length for ManagedToUnmanaged collections with NoCountInfo#126853
Draft
Fix marshalling generators using 0 as cleanup length for ManagedToUnmanaged collections with NoCountInfo#126853
Conversation
… arrays of jagged 2D arrays When the marshalling direction is ManagedToUnmanaged and there is no explicit count info (NoCountInfo), the cleanup code was setting numElements = 0, causing Free to never be called on inner array elements. This fix uses GetManagedValuesSource().Length instead to get the actual element count from the managed collection. Fixes #93423 Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/3665c6a3-655d-4215-a791-d3a838f80bd4 Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
…esSource Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/3665c6a3-655d-4215-a791-d3a838f80bd4 Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix marshalling cleanup for inner arrays of jagged 2D arrays
Fix marshalling generators using 0 as cleanup length for ManagedToUnmanaged collections with NoCountInfo
Apr 13, 2026
This was referenced Apr 14, 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.
Description
When marshalling collections ManagedToUnmanaged with no explicit count info (e.g., inner arrays of jagged 2D arrays with element marshallers that have
Free), the source generator emittednumElements = 0during cleanup. This caused the cleanup loop to never execute, leaking unmanaged memory on every successful call.The fix replaces the hardcoded
0withGetManagedValuesSource().Length, which is the correct element count for ManagedToUnmanaged cleanup — the managed collection is the source of truth.Before (generated cleanup code):
After:
Changes
ElementsMarshalling.cs— ExposeGenerateNumElementsAssignmentFromManagedValuesSource()on the base class, wrapping the existingfile staticextension method so it's callable fromStatelessMarshallingStrategy.csStatelessMarshallingStrategy.cs— ReplacenumElements = 0with the managed values source length inGenerateCleanupCallerAllocatedResourcesStatementsElementInWithFreemarshaller andNonBlittableElementWithFreeByValuetest cases for both stateless/stateful paths in LibraryImportGenerator and ComInterfaceGenerator unit tests