Skip to content

Commit

Permalink
Avoid some addref/release.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaanstra committed Nov 6, 2023
1 parent 50255b9 commit 2445e5b
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,18 @@ internal sealed class FactoryObjectReference<
{
private readonly IntPtr _contextToken;

internal FactoryObjectReference(IntPtr thisPtr) :
public static FactoryObjectReference<T> Attach(ref IntPtr thisPtr)
{
if (thisPtr == IntPtr.Zero)
{
return null;
}
var obj = new FactoryObjectReference<T>(thisPtr);
thisPtr = IntPtr.Zero;
return obj;
}

internal FactoryObjectReference(IntPtr thisPtr) :
base(thisPtr)
{
if (!IsFreeThreaded(this))
Expand Down Expand Up @@ -626,14 +637,16 @@ public static ObjectReference<I> Get<
if (activationFactory != null)
{
using (activationFactory)
#if NET
using (var objRef = activationFactory.As<I>(iid))
{
return FactoryObjectReference<I>.FromAbi(objRef.ThisPtr);
}
#if NET
if (activationFactory.TryAs(iid, out IntPtr iidPtr) >= 0)
{
return FactoryObjectReference<I>.Attach(ref iidPtr);
}
#else
return activationFactory.As<I>(iid);
return activationFactory.As<I>(iid);
#endif
}
}
}
}
Expand Down

0 comments on commit 2445e5b

Please sign in to comment.