From 36a22d2a561ee3306e3abab2fc8f8dc7fbfa74bf Mon Sep 17 00:00:00 2001 From: William Kent Date: Sun, 11 Jul 2021 15:47:01 -0400 Subject: [PATCH] Plug memory leak in StringMarshaller (#13) --- .../RuntimeCallableWrapperTest.cs | 1 + WinFormsComInterop.SourceGenerator/StringMarshaller.cs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/WinFormsComInterop.SourceGenerator.Tests/RuntimeCallableWrapperTest.cs b/WinFormsComInterop.SourceGenerator.Tests/RuntimeCallableWrapperTest.cs index 64d64eb..db9873e 100644 --- a/WinFormsComInterop.SourceGenerator.Tests/RuntimeCallableWrapperTest.cs +++ b/WinFormsComInterop.SourceGenerator.Tests/RuntimeCallableWrapperTest.cs @@ -1105,6 +1105,7 @@ unsafe partial class C : global::Foo.IStr Marshal.ThrowExceptionForHR(result); } + Marshal.FreeCoTaskMem(local_1); } finally { diff --git a/WinFormsComInterop.SourceGenerator/StringMarshaller.cs b/WinFormsComInterop.SourceGenerator/StringMarshaller.cs index 5b141be..11e7a67 100644 --- a/WinFormsComInterop.SourceGenerator/StringMarshaller.cs +++ b/WinFormsComInterop.SourceGenerator/StringMarshaller.cs @@ -59,6 +59,11 @@ public override void UnmarshalParameter(IndentedStringBuilder builder) { builder.AppendLine($"{Name} = Marshal.PtrToStringUni({LocalVariable});"); } + + if (RefKind != RefKind.Out && Index != -1) + { + builder.AppendLine($"Marshal.FreeCoTaskMem({LocalVariable});"); + } } } }