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

NullReferenceException in BitmapDecoder.SetupDecoderFromUriOrStream #10390

Open
bgrainger opened this issue Feb 4, 2025 · 1 comment
Open

Comments

@bgrainger
Copy link
Contributor

bgrainger commented Feb 4, 2025

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

@bgrainger
Copy link
Contributor Author

Suggested fix: change 3854d32 to

bitmapStream?.Close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant