Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taking a WeakReference to a C# object that inherits a native WinRT object crashes the app. #1025

Closed
jlaanstra opened this issue Oct 19, 2021 · 5 comments · Fixed by dotnet/runtime#61267
Labels
bug Something isn't working

Comments

@jlaanstra
Copy link
Collaborator

Describe the bug
WeakReferences to managed objects inheriting native objects causes InvalidCastExceptions and/or access violations.

To Reproduce

  1. Open the attached project in VS: WeakRefRepro.zip
  2. Click "Click Me" repeatedly.

Expected behavior
No crash/exception.

Version Info
CsWinRT 1.3.5

@jlaanstra jlaanstra added the bug Something isn't working label Oct 19, 2021
@manodasanW
Copy link
Member

Took a quick look and looks like it is failing to find the managed object:

WinRT_Runtime!WinRT.ComWrappersSupport.GetInspectableInfo+0x52 [D:\a\1\s\src\WinRT.Runtime\ComWrappersSupport.net5.cs @ 71] 
WinRT_Runtime!WinRT.IInspectable.Vftbl.Do_Abi_GetRuntimeClassName+0x36 [D:\a\1\s\src\WinRT.Runtime\IInspectable.cs @ 86] 
WinRT_Runtime!ILStubClass.IL_STUB_PInvoke(IntPtr, IntPtr*)+0x6d
WinRT_Runtime!WinRT.IInspectable.GetRuntimeClassName+0x8d [D:\a\1\s\src\WinRT.Runtime\IInspectable.cs @ 130] 
WinRT_Runtime!WinRT.ComWrappersSupport.GetRuntimeClassForTypeCreation+0x74 [D:\a\1\s\src\WinRT.Runtime\ComWrappersSupport.cs @ 368] 
WinRT_Runtime!WinRT.DefaultComWrappers.CreateObject+0x9b [D:\a\1\s\src\WinRT.Runtime\ComWrappersSupport.net5.cs @ 441] 
WinRT_Runtime!WinRT.DefaultComWrappers.CreateObject+0x48 [D:\a\1\s\src\WinRT.Runtime\ComWrappersSupport.net5.cs @ 470] 
System_Private_CoreLib!System.Runtime.InteropServices.ComWrappers.CallCreateObject+0x74 [/_/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs @ 268] 
coreclr!CallDescrWorkerInternal+0x83 [D:\workspace\_work\1\s\src\coreclr\src\vm\amd64\CallDescrWorkerAMD64.asm @ 100] 
coreclr!DispatchCallSimple+0x80 [D:\workspace\_work\1\s\src\coreclr\src\vm\callhelpers.cpp @ 222] 
coreclr!`anonymous namespace'::CallCreateObject+0x90 [D:\workspace\_work\1\s\src\coreclr\src\vm\interoplibinterface.cpp @ 564] 
coreclr!`anonymous namespace'::TryGetOrCreateObjectForComInstanceInternal+0x316 [D:\workspace\_work\1\s\src\coreclr\src\vm\interoplibinterface.cpp @ 835] 
coreclr!GlobalComWrappersForTrackerSupport::TryGetOrCreateObjectForComInstance+0x1e1 [D:\workspace\_work\1\s\src\coreclr\src\vm\interoplibinterface.cpp @ 1706] 
coreclr!LoadComWeakReferenceTarget+0x53e [D:\workspace\_work\1\s\src\coreclr\src\vm\weakreferencenative.cpp @ 282] 
coreclr!WeakReferenceOfTNative::GetTarget+0xc4b88 [D:\workspace\_work\1\s\src\coreclr\src\vm\weakreferencenative.cpp @ 725] 
System_Private_CoreLib!System.WeakReference<System.__Canon>.TryGetTarget+0xf [/_/src/libraries/System.Private.CoreLib/src/System/WeakReference.T.cs @ 57] 
WeakRefRepro!WeakRefRepro.InstancePerDeviceProviderBase<WeakRefRepro.DerivedClass>.GetInstanceForDevice+0x15c [E:\WeakRefRepro\WeakRefRepro\WeakRefRepro\WeakRefRepro\InstancePerObjectProviderBase.cs @ 32] 
WeakRefRepro!WeakRefRepro.MainWindow.Call+0x84 [E:\WeakRefRepro\WeakRefRepro\WeakRefRepro\WeakRefRepro\MainWindow.xaml.cs @ 41] 
WeakRefRepro!WeakRefRepro.MainWindow.<myButton_Click>d__2.MoveNext+0x4c [E:\WeakRefRepro\WeakRefRepro\WeakRefRepro\WeakRefRepro\MainWindow.xaml.cs @ 36] 

@manodasanW
Copy link
Member

manodasanW commented Oct 29, 2021

Another stack

WeakRefBase!winrt::impl::root_implements<winrt::WeakRefBase::implementation::Class,winrt::WeakRefBase::Class,winrt::WeakRefBase::IClassOverrides,winrt::composable>::QueryInterface+0xc [E:\WeakRefRepro\WeakRefRepro\WeakRefBase\Generated Files\winrt\base.h @ 7236] 
WeakRefBase!winrt::implements<winrt::WeakRefBase::implementation::Class,winrt::WeakRefBase::Class,winrt::WeakRefBase::IClassOverrides,winrt::composable>::QueryInterface+0xc [E:\WeakRefRepro\WeakRefRepro\WeakRefBase\Generated Files\winrt\base.h @ 7849] 
WeakRefBase!winrt::impl::produce_base<winrt::WeakRefBase::implementation::Class,winrt::WeakRefBase::IClass,void>::QueryInterface+0x1a [E:\WeakRefRepro\WeakRefRepro\WeakRefBase\Generated Files\winrt\base.h @ 6924] 
WeakRefBase!winrt::impl::weak_ref<1,1>::Resolve+0x36 [E:\WeakRefRepro\WeakRefRepro\WeakRefBase\Generated Files\winrt\base.h @ 7127] 
coreclr!LoadComWeakReferenceTarget+0x2e1 [D:\workspace\_work\1\s\src\coreclr\src\vm\weakreferencenative.cpp @ 263] 
coreclr!WeakReferenceOfTNative::GetTarget+0xc4b88 [D:\workspace\_work\1\s\src\coreclr\src\vm\weakreferencenative.cpp @ 725] 
System_Private_CoreLib!System.WeakReference<System.__Canon>.TryGetTarget+0xf [/_/src/libraries/System.Private.CoreLib/src/System/WeakReference.T.cs @ 57] 
WeakRefRepro!WeakRefRepro.InstancePerDeviceProviderBase<WeakRefRepro.DerivedClass>.GetInstanceForDevice+0x152 [E:\WeakRefRepro\WeakRefRepro\WeakRefRepro\WeakRefRepro\InstancePerObjectProviderBase.cs @ 32] 
WeakRefRepro!WeakRefRepro.MainWindow.Call+0x70 [E:\WeakRefRepro\WeakRefRepro\WeakRefRepro\WeakRefRepro\MainWindow.xaml.cs @ 41] 

@AaronRobinsonMSFT
Copy link
Member

AaronRobinsonMSFT commented Nov 5, 2021

@manodasanW This appears to be a .NET bug. @jkoritzinsky will be looking into a fix. We chatted offline and I believe we understand the problem—it is limited to aggregation scenarios where the native base type implements IWeakReferenceSource.

@jlaanstra
Copy link
Collaborator Author

@jkoritzinsky is there a .NET build I can use to verify this is fixed?

@jkoritzinsky
Copy link
Member

There's not a build available of the whole sdk, but this link should provide you with a .NET 7 nightly runtime build with the fix: https://dotnetcli.azureedge.net/dotnet/Runtime/7.0.0-alpha.1.21558.2/dotnet-runtime-7.0.0-alpha.1.21558.2-win-x64.exe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants