Skip to content

Fix marshalling generators using 0 as cleanup length for ManagedToUnmanaged collections with NoCountInfo#126853

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-jagged-array-marshalling
Draft

Fix marshalling generators using 0 as cleanup length for ManagedToUnmanaged collections with NoCountInfo#126853
Copilot wants to merge 3 commits intomainfrom
copilot/fix-jagged-array-marshalling

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 13, 2026

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 emitted numElements = 0 during cleanup. This caused the cleanup loop to never execute, leaking unmanaged memory on every successful call.

The fix replaces the hardcoded 0 with GetManagedValuesSource().Length, which is the correct element count for ManagedToUnmanaged cleanup — the managed collection is the source of truth.

Before (generated cleanup code):

__arg0__numElements = 0; // Free is never called on any element

After:

__arg0__numElements = Marshaller.GetManagedValuesSource(__arg0_managed).Length;

Changes

  • ElementsMarshalling.cs — Expose GenerateNumElementsAssignmentFromManagedValuesSource() on the base class, wrapping the existing file static extension method so it's callable from StatelessMarshallingStrategy.cs
  • StatelessMarshallingStrategy.cs — Replace numElements = 0 with the managed values source length in GenerateCleanupCallerAllocatedResourcesStatements
  • Test snippets — Add ElementInWithFree marshaller and NonBlittableElementWithFreeByValue test cases for both stateless/stateful paths in LibraryImportGenerator and ComInterfaceGenerator unit tests

Copilot AI review requested due to automatic review settings April 13, 2026 22:10
Copilot AI review requested due to automatic review settings April 13, 2026 22:10
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 13, 2026
… 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>
Copilot AI requested review from Copilot and removed request for Copilot April 13, 2026 22:42
…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 requested review from Copilot and removed request for Copilot April 13, 2026 22:45
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
Copilot AI requested a review from jtschuster April 13, 2026 22:46
@github-actions github-actions bot added area-System.Runtime.InteropServices and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Marshalling generators use 0 as the length during cleanup for inner arrays of jagged 2D arrays that are [In] and ManagedToUnmanaged

2 participants