mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.52k stars 538 forks source link

[BUG] AccessViolationException in finalizer thread #2194

Closed RichardD2 closed 2 years ago

RichardD2 commented 2 years ago

Description

After upgrading from v2.80.4-preview.9 to v2.88.0, I am now getting an uncatchable AccessViolationException in the finalizer thread, as described in the comment to #1817.

Code

The code is an ASP.NET MVC 5 application targeting .NET Framework 4.8. It uses QuestPDF v2022.6.3 to generate a PDF file. The only remotely complicated part of the code is for adding rounded corners to an image:

public class RoundedImage : IDynamicComponent<int>
{
    public RoundedImage([NotNull] string imagePath)
    {
        ImagePath = imagePath;
    }

    [NotNull]
    public string ImagePath { get; }

    int IDynamicComponent<int>.State { get; set; }

    [NotNull]
    private static SKBitmap LoadImage([NotNull] string imagePath)
    {
        using var stream = File.OpenRead(imagePath);
        return SKBitmap.Decode(stream);
    }

    private static float CalculateImageHeight([NotNull] string imagePath, float width)
    {
        using var image = LoadImage(imagePath);
        return width * image.Height / image.Width;
    }

    /// <inheritdoc />
    public DynamicComponentComposeResult Compose(DynamicContext context)
    {
        string imagePath = ImagePath;
        if (string.IsNullOrEmpty(imagePath) || !File.Exists(imagePath))
        {
            return new();
        }

        var content = context.CreateElement(container =>
        {
            var width = context.AvailableSize.Width;
            var imageHeight = CalculateImageHeight(imagePath, width);

            container.MaxHeight(imageHeight).Canvas((canvas, size) =>
            {
                canvas.Save();
                var canvasRect = SKRect.Create(0, 0, size.Width, size.Height);
                using SKPath path = new();
                path.AddRoundRect(canvasRect, 15f, 15f);
                canvas.ClipPath(path, antialias: true);
                using var image = LoadImage(imagePath);
                var imageRect = SKRect.Create(0, 0, image.Width, image.Height);
                canvas.DrawBitmap(image, imageRect, canvasRect);
                canvas.Restore();
            });
        });

        return new() { Content = content };
    }
}

The code works as expected, and the PDF is generated correctly.

Expected Behavior

No exception should be thrown in the finalizer thread.

Actual Behavior

An uncatchable exception is thrown in the finalizer thread.

Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
   at SkiaSharp.Internals.PlatformLock+NonAlertableWin32Lock.EnterCriticalSection(IntPtr)
   at SkiaSharp.Internals.PlatformLock+NonAlertableWin32Lock.EnterWriteLock()
   at SkiaSharp.HandleDictionary.DeregisterHandle(IntPtr, SkiaSharp.SKObject)
   at SkiaSharp.SKObject.DeregisterHandle(IntPtr, SkiaSharp.SKObject)
   at SkiaSharp.SKObject.set_Handle(IntPtr)
   at SkiaSharp.SKNativeObject.Dispose(Boolean)
   at SkiaSharp.SKNativeObject.Finalize()

Basic Information

Detailed IDE/OS information (click to expand) ``` Microsoft Visual Studio Professional 2022 Version 17.2.6 VisualStudio.17.Release/17.2.6+32630.192 Microsoft .NET Framework Version 4.8.04161 Installed Version: Professional .NET Core Debugging with WSL 1.0 .NET Core Debugging with WSL ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools ASA Service Provider 1.0 ASP.NET and Web Tools 2019 17.2.393.26812 ASP.NET and Web Tools 2019 ASP.NET Web Frameworks and Tools 2012 17.2.393.26812 For additional information, visit https://www.asp.net/ Azure App Service Tools v3.0.0 17.2.393.26812 Azure App Service Tools v3.0.0 Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio Azure Functions and Web Jobs Tools 17.2.393.26812 Azure Functions and Web Jobs Tools Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio Bundler & Minifier 2.9.3 Adds support for bundling and minifying JavaScript, CSS and HTML files in any project. C# Tools 4.2.0-4.22281.5+8d3180e5f00d42f0f0295165f756f368f0cbfa44 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Dotfuscator Community Edition 6.5.0+76d4669002 PreEmptive Protection - Dotfuscator CE Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. File Icons 2.7 Adds icons for files that are not recognized by Solution Explorer GhostDoc 22.2.22190.0 Generate XML Comments from your code, maintain clean and up-to-date documentation, produce help documentation in multiple formats, use intelligent source code Spell Checker in Visual Studio. Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics Microsoft Azure Tools for Visual Studio 2.9 Support for Azure Cloud Services projects Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Mono Debugging for Visual Studio 17.2.20 (482eb2a) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 6.2.1 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Razor (ASP.NET Core) 17.0.0.2218101+885a343b00bcab620a90c1550c37dafd730ce984 Provides languages services for ASP.NET Core Razor. Sandcastle Help File Builder SHFB Visual Studio integration for the Sandcastle Help File Builder. https://GitHub.com/EWSoftware/SHFB SQL Server Data Tools 17.0.62204.01010 Microsoft SQL Server Data Tools Syntax Visualizer 1.0 An extension for visualizing Roslyn SyntaxTrees. ToolWindowHostedEditor 1.0 Hosting json editor into a tool window TypeScript Tools 17.0.10418.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 4.2.0-4.22281.5+8d3180e5f00d42f0f0295165f756f368f0cbfa44 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual F# Tools 17.1.0-beta.22329.1+702b8e77f5fbfe21e6743324c1750503e02f182d Microsoft Visual F# Tools Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio. VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio Web Compiler 1.14.8 Compiler for LESS, Sass and CoffeeScript files Xamarin 17.2.0.177 (d17-2@2f6c881) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 17.2.0.244 (remotes/origin/d17-2@197e1a0b7) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 17.2.15 (2e3b60e) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 12.3.3.3 (d17-2/4e061b7) Xamarin.Android Reference Assemblies and MSBuild support. Mono: dffa5ab Java.Interop: xamarin/java.interop/d17-2@9760f0a9 ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.38.2@7b1e016 Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-2@fc3c2ac Xamarin.iOS and Xamarin.Mac SDK 15.10.0.5 (96b3edb6d) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```
mattleibow commented 2 years ago

May also be related to https://github.com/mono/SkiaSharp/issues/2181?