Skip to content

Commit

Permalink
Fix for aggregated tear off scenarios (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
manodasanW authored Jun 11, 2021
1 parent d4a60fd commit 9154452
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/WinRT.Runtime/MatchingRefApiCompatBaseline.net5.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ MembersMustExist : Member 'public void WinRT.ComWrappersSupport.RegisterObjectFo
MembersMustExist : Member 'protected void WinRT.IObjectReference.AddRef(System.Boolean)' does not exist in the reference but it does exist in the implementation.
TypesMustExist : Type 'WinRT.Interop.IWeakReference' does not exist in the reference but it does exist in the implementation.
TypesMustExist : Type 'WinRT.Interop.IWeakReferenceSource' does not exist in the reference but it does exist in the implementation.
Total Issues: 6
MembersMustExist : Member 'public System.Int32 WinRT.IObjectReference.TryAs(System.Guid, System.IntPtr)' does not exist in the reference but it does exist in the implementation.
Total Issues: 7
17 changes: 17 additions & 0 deletions src/WinRT.Runtime/ObjectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ public virtual unsafe int TryAs<T>(Guid iid, out ObjectReference<T> objRef)
objRef.ReferenceTrackerPtr = ReferenceTrackerPtr;
}
return hr;
}

// Used only as part of the GetInterface implementation where the
// result is an reference passed across the ABI and doesn't need to
// be tracked as an internal reference. This is separate to handle
// tear off aggregate scenario where releasing an reference can end up
// deleting the tear off interface.
public virtual unsafe int TryAs(Guid iid, out IntPtr ppv)
{
ppv = IntPtr.Zero;
ThrowIfDisposed();
int hr = VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr);
if (hr >= 0)
{
ppv = thatPtr;
}
return hr;
}

public unsafe IObjectReference As(Guid iid) => As<IUnknownVftbl>(iid);
Expand Down
6 changes: 1 addition & 5 deletions src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5135,14 +5135,10 @@ if (IsOverridableInterface(iid) || typeof(global::WinRT.IInspectable).GUID == ii
return global::System.Runtime.InteropServices.CustomQueryInterfaceResult.NotHandled;
}
if (%.TryAs<IUnknownVftbl>(iid, out ObjectReference<IUnknownVftbl> objRef) >= 0)
if (%.TryAs(iid, out ppv) >= 0)
{
using (objRef)
{
ppv = objRef.GetRef();
return global::System.Runtime.InteropServices.CustomQueryInterfaceResult.Handled;
}
}
return global::System.Runtime.InteropServices.CustomQueryInterfaceResult.NotHandled;
})",
Expand Down

0 comments on commit 9154452

Please sign in to comment.