[release/7.0-staging] Fix a memory leak in runtime interop stubs when using an array of structs of types that use old-style managed marshalers #93148
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.
Backport of #93089 to release/7.0-staging
/cc @jkoritzinsky
Description
When passing an array of structs that contains an array field marshalled as a SAFEARRAY or "ByValArray" field to a P/Invoke or a COM interop call, the native array and its contents are not released. This PR changes the marshalling logic such that the native array and contents will be released when they should be.
Customer Impact
Customers without this fix that have interop code with this shape will have unmanaged memory leaks.
Regression
This is a regression that was introduced in .NET 5.
Testing
This fix has been manually validated in main and a unit test has been added to the PR in main. This fix has also manually been validated in the 6.0 branch with the customer and it fixes their scenario.
Risk
The risk for this PR is low. The logic here will only be hit in these two scenarios, and the expected and documented behavior is that we do not leak this memory. It is very unlikely that customers have taken a dependency on this memory being leaked.