Skip to content

Commit

Permalink
Add null checks to some 'Marshal.Release' calls (#1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 authored Oct 17, 2024
1 parent 0a18c1f commit 69ef417
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/AgileReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private static unsafe ABI.WinRT.Interop.IGlobalInterfaceTable GetGitTable()
}
finally
{
Marshal.Release(gitPtr);
MarshalExtensions.ReleaseIfNotNull(gitPtr);
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,7 @@ private static object CreateObject(IntPtr externalComObject)
}
finally
{
if (ptr != IntPtr.Zero)
{
Marshal.Release(ptr);
}
MarshalExtensions.ReleaseIfNotNull(ptr);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ namespace WinRT
{
internal static class MarshalExtensions
{
/// <summary>
/// Releases a COM object, if not <see langword="null"/>.
/// </summary>
/// <param name="pUnk">The input COM object to release.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void ReleaseIfNotNull(IntPtr pUnk)
{
if ((void*)pUnk == null)
{
return;
}

_ = ((delegate* unmanaged[Stdcall]<IntPtr, int>)(*(*(void***)pUnk + 2 /* IUnknown.Release slot */)))(pUnk);
}

public static void Dispose(this GCHandle handle)
{
if (handle.IsAllocated)
Expand Down
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/Projections/IReferenceArray.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static unsafe object GetValue(IInspectable inspectable)
finally
{
Marshaler<T>.DisposeAbiArray((__retval_length, __retval_data));
Marshal.Release(referenceArrayPtr);
MarshalExtensions.ReleaseIfNotNull(referenceArrayPtr);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static ObjectReferenceValue CreateMarshaler2(global::System.Collections.S
{
MarshalInterface<global::System.Collections.IList>.DisposeAbi(newItems);
MarshalInterface<global::System.Collections.IList>.DisposeAbi(oldItems);
Marshal.Release(thisPtr);
MarshalExtensions.ReleaseIfNotNull(thisPtr);
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/WinRT.Runtime/Projections/Nullable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public static unsafe T GetValue(IInspectable inspectable)
}
finally
{
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}

Expand Down Expand Up @@ -630,7 +630,7 @@ public static unsafe Nullable GetValue(IInspectable inspectable)
finally
{
MarshalString.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -1427,7 +1427,7 @@ public static unsafe object GetValue(IInspectable inspectable)
finally
{
DateTimeOffset.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -1504,7 +1504,7 @@ public static unsafe object GetValue(IInspectable inspectable)
finally
{
TimeSpan.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -1641,7 +1641,7 @@ public static unsafe Nullable GetValue(IInspectable inspectable)
finally
{
Type.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -1724,7 +1724,7 @@ public static unsafe Nullable GetValue(IInspectable inspectable)
finally
{
Exception.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -1784,7 +1784,7 @@ public static unsafe Nullable GetValue(IInspectable inspectable)
finally
{
EventHandler.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -1849,7 +1849,7 @@ public static unsafe Nullable GetValue(IInspectable inspectable)
finally
{
global::ABI.System.ComponentModel.PropertyChangedEventHandler.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -1914,7 +1914,7 @@ public static unsafe Nullable GetValue(IInspectable inspectable)
finally
{
global::ABI.System.Collections.Specialized.NotifyCollectionChangedEventHandler.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -1977,7 +1977,7 @@ public static unsafe Nullable GetValue(IInspectable inspectable)
finally
{
Marshaler<T>.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -2039,7 +2039,7 @@ internal static unsafe object GetValue(global::System.Type enumType, IInspectabl
}
finally
{
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -2101,7 +2101,7 @@ internal static unsafe object GetValue(global::System.Type enumType, IInspectabl
}
finally
{
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -2129,7 +2129,7 @@ public static unsafe object GetValue(IInspectable inspectable)
}
finally
{
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}
}
Expand Down Expand Up @@ -2387,7 +2387,7 @@ unsafe object IWinRTNullableTypeDetails.GetNullableValue(IInspectable inspectabl
finally
{
Marshaler<T>.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}

Expand Down Expand Up @@ -2443,7 +2443,7 @@ unsafe object IWinRTNullableTypeDetails.GetNullableValue(IInspectable inspectabl
finally
{
Marshaler<T>.DisposeAbi(__retval);
Marshal.Release(nullablePtr);
MarshalExtensions.ReleaseIfNotNull(nullablePtr);
}
}

Expand Down

0 comments on commit 69ef417

Please sign in to comment.