Skip to content
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

Fix GC hole in string marshaling #773

Merged
merged 4 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/WinRT.Runtime/ApiCompatBaseline.net5.0.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Total Issues: 0
MembersMustExist : Member 'public WinRT.MarshalString.HStringHeader WinRT.MarshalString.HStringHeader WinRT.MarshalString._header' does not exist in the implementation but it does exist in the contract.
TypesMustExist : Type 'WinRT.MarshalString.HStringHeader' does not exist in the implementation but it does exist in the contract.
13 changes: 6 additions & 7 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ public static void Dispose(this GCHandle handle)
// as well as passing marshalers to FromAbi by ref so they can be conditionally disposed.
public class MarshalString
{
public unsafe struct HStringHeader // sizeof(HSTRING_HEADER)
{
public fixed byte Reserved[24];
};
public HStringHeader _header;
private IntPtr _header;
public GCHandle _gchandle;
public IntPtr _handle;

public void Dispose()
{
_gchandle.Dispose();
Marshal.FreeHGlobal(_header);
_header = IntPtr.Zero;
}

public static unsafe MarshalString CreateMarshaler(string value)
Expand All @@ -54,10 +52,11 @@ public static unsafe MarshalString CreateMarshaler(string value)
try
{
m._gchandle = GCHandle.Alloc(value, GCHandleType.Pinned);
fixed (void* chars = value, header = &m._header, handle = &m._handle)
m._header = Marshal.AllocHGlobal(24); // sizeof(HSTRING_HEADER)
fixed (void* chars = value, handle = &m._handle)
{
Marshal.ThrowExceptionForHR(Platform.WindowsCreateStringReference(
(char*)chars, value.Length, (IntPtr*)header, (IntPtr*)handle));
(char*)chars, value.Length, (IntPtr*)m._header, (IntPtr*)handle));
};
return m;
}
Expand Down
6 changes: 2 additions & 4 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,16 @@ public static unsafe (ObjectReference<IActivationFactoryVftbl> obj, int hr) GetA
{
// TODO: "using var" with ref struct and remove the try/catch below
var m = MarshalString.CreateMarshaler(runtimeClassId);
Func<bool> dispose = () => { m.Dispose(); return false; };
try
{
IntPtr instancePtr;
int hr;
(instancePtr, hr) = GetActivationFactory(MarshalString.GetAbi(m));
return (hr == 0 ? ObjectReference<IActivationFactoryVftbl>.Attach(ref instancePtr) : null, hr);
}
catch (Exception) when (dispose())
finally
{
// Will never execute
return default;
m.Dispose();
}
}

Expand Down