Skip to content

Commit

Permalink
Fixed ObjectDisposedException on delegates (#674)
Browse files Browse the repository at this point in the history
* Fix ObjectDisposedException by resurrecting object reference of managed object which was returned.

* Minor change.
  • Loading branch information
manodasanW authored Jan 19, 2021
1 parent 3a4dd0d commit 913244d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ public static bool TryUnwrapObject(object o, out IObjectReference objRef)

public static void RegisterObjectForInterface(object obj, IntPtr thisPtr) => TryRegisterObjectForInterface(obj, thisPtr);

public static object TryRegisterObjectForInterface(object obj, IntPtr thisPtr) => ComWrappers.GetOrRegisterObjectForComInstance(thisPtr, CreateObjectFlags.TrackerObject, obj);
public static object TryRegisterObjectForInterface(object obj, IntPtr thisPtr)
{
var rcw = ComWrappers.GetOrRegisterObjectForComInstance(thisPtr, CreateObjectFlags.TrackerObject, obj);

// Resurrect IWinRTObject's disposed IObjectReferences, if necessary
var target = rcw is Delegate del ? del.Target : rcw;
if (target is IWinRTObject winrtObj)
{
winrtObj.Resurrect();
}
return rcw;
}

public static IObjectReference CreateCCWForObject(object obj)
{
Expand Down

0 comments on commit 913244d

Please sign in to comment.