microsoft / CsWinRT

C# language projection for the Windows Runtime
MIT License
553 stars 105 forks source link

Loading images using .net file streams does not work when AOT is enabled #1634

Closed lhak closed 4 months ago

lhak commented 5 months ago

Describe the bug

Calling BitmapImage.SetSourceAsync() with a .net file stream using stream.AsRandomAccessStream() leads to WinRT originate error - 0x80004002 : 'Specified cast is not valid.'

To Reproduce

Code similar to the one below:

using (FileStream stream = File.OpenRead(path))
{
  bitmapImage = new BitmapImage();
  await bitmapImage.SetSourceAsync(stream.AsRandomAccessStream());
}

works correctly when AOT is disabled but not when it is enabled

Expected behavior

.net file streams should work when AOT is enabled

Version Info

appsdk 1.6 experimental 1, CsWinRT 2.1.0-prerelease.240602.1

Additional context

dongle-the-gadget commented 5 months ago

@manodasanW Is there a reason why NetFxToWinRtStreamAdapter is said to must not inherit directly from IRandomAccessStream?

lhak commented 4 months ago

Just wanted to add the stack trace of the crash:

App.exe!WinRT_Runtime_WinRT_ComWrappersSupport__CreateCCWForObjectForABI() Line 170 
App.exe!WinRT_Runtime_WinRT_ComWrappersSupport__CreateCCWForObjectForMarshaling() Line 1187 
App.exe!WinRT_Runtime_WinRT_MarshalInspectable_1<System___Canon>__CreateMarshaler2() Line 1789  
App.exe!WinRT_Runtime_WinRT_MarshalInterface_1<System___Canon>__CreateMarshaler2() Line 1619    
App.exe!WinRT_Runtime_WinRT_MarshalInterface_1<System___Canon>__FromManaged() Line 1647 
App.exe!Microsoft_Windows_SDK_NET_ABI_Windows_Storage_Streams_IInputStream__Do_Abi_ReadAsync_0()    
Microsoft.ui.xaml.dll!DirectUI::BitmapSource::SetSourceShared(ABI::Windows::Storage::Streams::IRandomAccessStream * pStreamSource) Line 172
Microsoft.ui.xaml.dll!DirectUI::BitmapSource::SetSourceImpl(ABI::Windows::Storage::Streams::IRandomAccessStream * pStreamSource) Line 225
Microsoft.ui.xaml.dll!DirectUI::BitmapSourceGenerated::SetSource(ABI::Windows::Storage::Streams::IRandomAccessStream * pStreamSource) Line 66
Microsoft.ui.xaml.dll!DirectUI::BitmapImage::SetSource(ABI::Windows::Storage::Streams::IRandomAccessStream * pStreamSource) Line 64

The obj parameter in CreateCCWForObjectForABI() is of type Microsoft_Windows_SDK_NET_System_Threading_Tasks_TaskToAsyncOperationWithProgressAdapter_2