Skip to content

Commit

Permalink
Add fallback for when an interface is kept alive but its Vftbl type w…
Browse files Browse the repository at this point in the history
…as linked away since no members on the interface were called.
  • Loading branch information
jkoritzinsky committed Aug 13, 2020
1 parent 6e455f3 commit 686cead
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion WinRT.Runtime/IWinRTObject.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfac
{
return false;
}
var vftblType = helperType.GetNestedType("Vftbl");
int hr = NativeObject.TryAs<IUnknownVftbl>(GuidGenerator.GetIID(helperType), out var objRef);
if (hr < 0)
{
Expand All @@ -25,6 +24,20 @@ bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfac
}
return false;
}
var vftblType = helperType.GetNestedType("Vftbl");
if (vftblType is null)
{
// The helper type might not have a vftbl type if it was linked away.
// The only time the Vftbl type would be linked away is when we don't actually use
// any of the methods on the interface (it was just a type cast/"is Type" check).
// In that case, we can use the IUnknownVftbl-typed ObjectReference since
// it has all of the information we'll need.
if (!QueryInterfaceCache.TryAdd(interfaceType, objRef))
{
objRef.Dispose();
}
return true;
}
using (objRef)
{
IObjectReference typedObjRef = (IObjectReference)typeof(IObjectReference).GetMethod("As", Type.EmptyTypes).MakeGenericMethod(vftblType).Invoke(objRef, null);
Expand Down

0 comments on commit 686cead

Please sign in to comment.