-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MARSHALLED_UNICODE_STRING[] array marshalling leaks memory #76584
Comments
Tagging subscribers to this area: @dotnet/interop-contrib Issue DetailsThe marshalling code generated for runtime/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaLookupNames2.cs Lines 13 to 21 in 0c631ab
names argument, the remaining arguments omitted for brevity):
// <auto-generated/>
unsafe partial class Test
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "7.0.6.47809")]
[System.Runtime.CompilerServices.SkipLocalsInitAttribute]
internal static partial uint LsaLookupNames2(global::Test.MARSHALLED_UNICODE_STRING[] names)
{
int __lastError;
global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native* __names_native = default;
uint __retVal;
// Setup - Perform required setup.
global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller<global::Test.MARSHALLED_UNICODE_STRING, global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native>.ManagedToUnmanagedIn __names_native__marshaller = new();
try
{
// Marshal - Convert managed data to native data.
global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native* __names_native__stackptr = stackalloc global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller<global::Test.MARSHALLED_UNICODE_STRING, global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native>.ManagedToUnmanagedIn.BufferSize];
__names_native__marshaller.FromManaged(names, new System.Span<global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native>(__names_native__stackptr, global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller<global::Test.MARSHALLED_UNICODE_STRING, global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native>.ManagedToUnmanagedIn.BufferSize));
{
System.ReadOnlySpan<global::Test.MARSHALLED_UNICODE_STRING> __names_native__managedSpan = __names_native__marshaller.GetManagedValuesSource();
System.Span<global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native> __names_native__nativeSpan = __names_native__marshaller.GetUnmanagedValuesDestination();
for (int __i0 = 0; __i0 < __names_native__managedSpan.Length; ++__i0)
{
__names_native__nativeSpan[__i0] = global::Test.MARSHALLED_UNICODE_STRING.Marshaller.ConvertToUnmanaged(__names_native__managedSpan[__i0]);
}
}
// Pin - Pin data in preparation for calling the P/Invoke.
fixed (void* __names_native__unused = __names_native__marshaller)
{
// PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned.
__names_native = __names_native__marshaller.ToUnmanaged();
System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
__retVal = __PInvoke(__names_native);
__lastError = System.Runtime.InteropServices.Marshal.GetLastSystemError();
}
}
finally
{
// Cleanup - Perform required cleanup.
__names_native__marshaller.Free();
}
System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
return __retVal;
// Local P/Invoke
[System.Runtime.InteropServices.DllImportAttribute("Advapi32", EntryPoint = "LsaLookupNames2", ExactSpelling = true)]
static extern unsafe uint __PInvoke(global::Test.MARSHALLED_UNICODE_STRING.Marshaller.Native* names);
}
} Notice that there is no cleanup of the unmanaged array after the PInvoke. Are the unmanaged string copies leaking?
|
Found by review of ccd67b0#diff-0004b77bd28bade5f8de26f55217352ec12d8af3c7de3d2c344f8e4ff8c38cebR51 prompted by #76538 |
Other places that marshal array using non-trivial marshallers seem to have the same problem. |
Yes. This looks like we are missing the element clean-up. @jkoritzinsky Can you take a look at why the container is cleaned up but we don't do the same for the elements? runtime/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaLookupNames2.cs Lines 23 to 55 in c075692
|
Agreed. Perhaps the |
I expect we missed doing it in our marshalling strategy for collections with non-blittable elements. Line 490 in 4ca326f
|
The marshalling code generated for
runtime/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaLookupNames2.cs
Lines 13 to 21 in 0c631ab
names
argument, the remaining arguments omitted for brevity):Notice that there is no cleanup of the unmanaged array after the PInvoke. Are the unmanaged string copies leaking?
The text was updated successfully, but these errors were encountered: