Skip to content

Commit

Permalink
Use Marshal.AddRef/Release/QueryInterface. (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaanstra authored Nov 11, 2023
1 parent e14f5d5 commit 0c65b63
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/WinRT.Runtime/ObjectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private unsafe uint RefCount
{
get
{
VftblIUnknown.AddRef(ThisPtr);
return VftblIUnknown.Release(ThisPtr);
Marshal.AddRef(ThisPtr);
return (uint)Marshal.Release(ThisPtr);
}
}

Expand All @@ -76,7 +76,7 @@ internal unsafe IntPtr ReferenceTrackerPtr
_referenceTrackerPtr = value;
if (_referenceTrackerPtr != IntPtr.Zero)
{
ReferenceTracker.IUnknownVftbl.AddRef(_referenceTrackerPtr);
Marshal.AddRef(_referenceTrackerPtr);
AddRefFromTrackerSource();
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public unsafe TInterface AsInterface<TInterface>()
{
Guid iid = typeof(TInterface).GUID;
IntPtr comPtr = IntPtr.Zero;
Marshal.ThrowExceptionForHR(VftblIUnknown.QueryInterface(ThisPtr, &iid, &comPtr));
Marshal.ThrowExceptionForHR(Marshal.QueryInterface(ThisPtr, ref iid, out comPtr));
try
{
return (TInterface)Marshal.GetObjectForIUnknown(comPtr);
Expand Down Expand Up @@ -180,7 +180,7 @@ public virtual unsafe int TryAs<
objRef = null;
ThrowIfDisposed();
IntPtr thatPtr = IntPtr.Zero;
int hr = VftblIUnknown.QueryInterface(ThisPtr, &iid, &thatPtr);
int hr = Marshal.QueryInterface(ThisPtr, ref iid, out thatPtr);
if (hr >= 0)
{
if (IsAggregated)
Expand Down Expand Up @@ -217,7 +217,7 @@ public virtual unsafe int TryAs(Guid iid, out IntPtr ppv)
ppv = IntPtr.Zero;
ThrowIfDisposed();
IntPtr thatPtr = IntPtr.Zero;
int hr = VftblIUnknown.QueryInterface(ThisPtr, &iid, &thatPtr);
int hr = Marshal.QueryInterface(ThisPtr, ref iid, out thatPtr);
if (hr >= 0)
{
ppv = thatPtr;
Expand Down Expand Up @@ -305,7 +305,7 @@ internal bool Resurrect()

protected virtual unsafe void AddRef(bool refFromTrackerSource)
{
VftblIUnknown.AddRef(ThisPtr);
Marshal.AddRef(ThisPtr);
if(refFromTrackerSource)
{
AddRefFromTrackerSource();
Expand All @@ -320,13 +320,13 @@ protected virtual unsafe void AddRef()
protected virtual unsafe void Release()
{
ReleaseFromTrackerSource();
VftblIUnknown.Release(ThisPtr);
Marshal.Release(ThisPtr);
}

private protected unsafe void ReleaseWithoutContext()
{
ReleaseFromTrackerSource();
VftblIUnknownFromOriginalContext.Release(ThisPtrFromOriginalContext);
Marshal.Release(ThisPtrFromOriginalContext);
}

internal unsafe bool IsReferenceToManagedObject
Expand Down Expand Up @@ -357,7 +357,7 @@ private unsafe void ResurrectTrackerSource()
{
if (ReferenceTrackerPtr != IntPtr.Zero)
{
ReferenceTracker.IUnknownVftbl.AddRef(ReferenceTrackerPtr);
Marshal.AddRef(ReferenceTrackerPtr);
if (!PreventReleaseFromTrackerSourceOnDispose)
{
ReferenceTracker.AddRefFromTrackerSource(ReferenceTrackerPtr);
Expand All @@ -373,7 +373,7 @@ private unsafe void DisposeTrackerSource()
{
ReferenceTracker.ReleaseFromTrackerSource(ReferenceTrackerPtr);
}
ReferenceTracker.IUnknownVftbl.Release(ReferenceTrackerPtr);
Marshal.Release(ReferenceTrackerPtr);
}
}

Expand All @@ -391,7 +391,7 @@ public ObjectReferenceValue AsValue()
public unsafe ObjectReferenceValue AsValue(Guid iid)
{
IntPtr thatPtr = IntPtr.Zero;
Marshal.ThrowExceptionForHR(VftblIUnknown.QueryInterface(ThisPtr, &iid, &thatPtr));
Marshal.ThrowExceptionForHR(Marshal.QueryInterface(ThisPtr, ref iid, out thatPtr));
if (IsAggregated)
{
Marshal.Release(thatPtr);
Expand Down Expand Up @@ -451,8 +451,8 @@ public static unsafe ObjectReference<T> FromAbi(IntPtr thisPtr, T vftblT)
{
return null;
}
Marshal.AddRef(thisPtr);
var obj = new ObjectReference<T>(thisPtr, vftblT);
obj.VftblIUnknown.AddRef(obj.ThisPtr);
return obj;
}

Expand Down Expand Up @@ -646,7 +646,7 @@ public override unsafe int TryAs<
objRef = null;

IntPtr thatPtr = IntPtr.Zero;
int hr = VftblIUnknown.QueryInterface(ThisPtr, &iid, &thatPtr);
int hr = Marshal.QueryInterface(ThisPtr, ref iid, out thatPtr);
if (hr >= 0)
{
if (IsAggregated)
Expand Down Expand Up @@ -710,7 +710,7 @@ public unsafe readonly IntPtr Detach()
// If the ptr is not owned by this instance, do an AddRef.
if (preventReleaseOnDispose && ptr != IntPtr.Zero)
{
(**(IUnknownVftbl**)ptr).AddRef(ptr);
Marshal.AddRef(ptr);
}

// Release tracker source reference as it is no longer a managed ref maintained by RCW.
Expand All @@ -731,7 +731,7 @@ public unsafe readonly void Dispose()

if (!preventReleaseOnDispose && ptr != IntPtr.Zero)
{
(**(IUnknownVftbl**)ptr).Release(ptr);
Marshal.Release(ptr);
}
}
}
Expand Down

0 comments on commit 0c65b63

Please sign in to comment.