microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

Microsoft.UI.Xaml.Controls.Image causes an application crash #9765

Closed megamechanic closed 2 weeks ago

megamechanic commented 3 months ago

Describe the bug

using <Image Source="{Binding Url}" /> causes random application crash. This sometimes happens in a random place in the application for different users, regardless of the SDK version & OS verision.

U can see this in Event Viewer only: Faulting Application Name: Symplast.Practice.WinUI.exe, Version: 1.0.0.0, Timestamp: 0x661e0000 Faulting module name: KERNELBASE.dll, version: 10.0.19041.4355, timestamp: 0xd7762934 Exception code: 0xc000027b Error offset: 0x000000000012d332 Faulting process ID: 0x4260 Faulty application startup time: 0x01dab36dba2d6f93 Faulting application path: C:\dev\Repos\WindowsPracticeApp\src\Symplast.Practice.WinUI\bin\x64\Debug\net7.0-windows10.0.22000.0\win10-x64\AppX\Symplast.Practice.WinUI.exe Path of the failed module: C:\WINDOWS\System32\KERNELBASE.dll Report ID: 2a467cce-ee5d-4574-94e5-529b2174cec6 Full name of the failing package: SymplastAcquisitionInc.SymplastPracticeDebug_3.406.0.0_x64__0fs0te9py8mmr Application code associated with the failing package: App

If you subscribe to Image.Loaded event and assign Image.Source in code It will fix the crash. However it doesn't help when ItemsStackPanel because of virtualization.

Steps to reproduce the bug

add<Image Source="{Binding Url}" /> to any place in an app

Expected behavior

No response

Screenshots

No response

NuGet package version

Windows App SDK 1.5.3: 1.5.240428000

Packaging type

Packaged (MSIX)

Windows version

No response

IDE

Visual Studio 2022

Additional context

for ex: using this custom control instead of Image fix the crash

public class ImageControl : UserControl
{
    public static readonly DependencyProperty SourceProperty =
           DependencyProperty.Register(nameof(Source), typeof(ImageSource), typeof(ImageControl)
           , new PropertyMetadata(null, (s, e) => ((ImageControl)s).OnSourcePropertyChanged(e)));

    public static readonly DependencyProperty StretchProperty =
            DependencyProperty.Register(nameof(Stretch), typeof(Stretch), typeof(ImageControl), new(Stretch.Uniform));

    private Image? _internalImage;

    public event ExceptionRoutedEventHandler? InternalImageFailed;

    public event RoutedEventHandler? InternalImageLoaded;

    public event RoutedEventHandler? InternalImageOpened;

    public Image? InternalImage
    {
        get => _internalImage;
        private set
        {
            _internalImage = value;
            Content = _internalImage;
        }
    } 

    public ImageSource? Source
    {
        get => GetValue(SourceProperty) as ImageSource;
        set => SetValue(SourceProperty, value);
    }

    public Stretch Stretch
    {
        get => (Stretch)GetValue(StretchProperty);
        set => SetValue(StretchProperty, value);
    }

    private Image CreateImageContent()
    {
        Image image = new()
        {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch
        };

        var binding = new Binding()
        {
            Path = new PropertyPath(nameof(Width)),
            Source = this,
            Mode = BindingMode.OneWay,
        };

        image.SetBinding(Image.WidthProperty, binding);

        binding = new Binding()
        {
            Path = new PropertyPath(nameof(Height)),
            Source = this,
            Mode = BindingMode.OneWay,
        };

        image.SetBinding(Image.HeightProperty, binding);

        binding = new Binding()
        {
            Path = new PropertyPath(nameof(MinHeight)),
            Source = this,
            Mode = BindingMode.OneWay,
        };

        image.SetBinding(Image.MinHeightProperty, binding);

        binding = new Binding()
        {
            Path = new PropertyPath(nameof(MinWidth)),
            Source = this,
            Mode = BindingMode.OneWay,
        };

        image.SetBinding(Image.MinWidthProperty, binding);

        binding = new Binding()
        {
            Path = new PropertyPath(nameof(Stretch)),
            Source = this,
            Mode = BindingMode.OneWay,
        };

        image.SetBinding(Image.StretchProperty, binding);

        return image;
    }

    private void OnInternalImageFailed(object sender, ExceptionRoutedEventArgs e) => InternalImageFailed?.Invoke(sender, e);

    private void OnInternalImageLoaded(object sender, RoutedEventArgs e)
    {
        TrySetSource();
        InternalImageLoaded?.Invoke(sender, e);
    }

    private void OnInternalImageOpened(object sender, RoutedEventArgs e) => InternalImageOpened?.Invoke(sender, e);

    private void OnIsImagePropertyPropertyChanged(DependencyPropertyChangedEventArgs e) => TrySetSource();

    private void OnSourcePropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        if (InternalImage != null)
        {
            InternalImage.Loaded -= OnInternalImageLoaded;
            InternalImage.ImageOpened -= OnInternalImageOpened;
            InternalImage.ImageFailed -= OnInternalImageFailed;

            InternalImage = null;
        }

        if (Source != null)
        {
            var image = CreateImageContent();

            image.Loaded += OnInternalImageLoaded;
            image.ImageOpened += OnInternalImageOpened;
            image.ImageFailed += OnInternalImageFailed;

            InternalImage = image;
        }
    }

    private void TrySetSource()
    {
        if (InternalImage != null && InternalImage.IsLoaded)
        {
            InternalImage.Source = Source;
        }
    }
}
github-actions[bot] commented 2 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

kmahone commented 1 month ago

I was unable to reproduce this issue. Could you please share a simple project that demonstrates this issue?

megamechanic commented 1 month ago

I was unable to reproduce this issue. Could you please share a simple project that demonstrates this issue?

I'd tried but I couldn't create a simple project.

ranjeshj commented 2 weeks ago

Unfortunately we are not able to reproduce the issue making this issue unactionable. Please provide a repro if you can reproduce it. Thanks!