Description
Description
Since updating our application to .NET 9 (from .NET 8) we have started receiving these crash reports from the field:
Message: NullReferenceException: Object reference not set to an instance of an object. in
System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle):501
System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache):191
System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
System.Windows.Media.Imaging.BitmapImage.EndInit():124
(user code)
Reproduction Steps
Load an invalid image using BitmapImage.StreamSource
:
using var stream = new MemoryStream("invalid image"u8.ToArray()); // any non-image data
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
Expected behavior
Throws a NotSupportedException
(or a similar exception type) to indicate that the user is attempting to load bad data.
Actual behavior
It throws a NullReferenceException
in WPF, which indicates a bug in WPF.
Regression?
Yes. In .NET 8.0, this threw a NotSupportedException: No imaging component suitable to complete this operation was found.
. The change of exception type is a breaking API change. (Our code was catching NotSupportedException
, so the new NullReferenceException
went unhandled. It is unexpected for framework code to throw NullReferenceException
.)
It's most likely caused by this change 3854d32 from #8174 fixing #2881.
Known Workarounds
Catch and ignore the NullReferenceException
.
Impact
Application crash in previously working code.
Configuration
.NET 9.0.1
Windows 11 (10.0.26100.0)
x64
Other information
No response