SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

[BUG] SKImage.FromPicture throws System.AccessViolationException in V3, it works in V2

Open StipoR opened this issue 1 year ago • 3 comments

Description

SKImage.FromPicture method throws System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

Call Stack:

[Managed to Native Transition]	
SkiaSharp.dll!SkiaSharp.SkiaApi.sk_image_new_from_picture(nint picture, SkiaSharp.SKSizeI* dimensions, SkiaSharp.SKMatrix* cmatrix, nint paint, bool useFloatingPointBitDepth, nint colorSpace, nint props)	Unknown
SkiaSharp.dll!SkiaSharp.SKImage.FromPicture(SkiaSharp.SKPicture picture, SkiaSharp.SKSizeI dimensions, SkiaSharp.SKMatrix* matrix, SkiaSharp.SKPaint paint, bool useFloatingPointBitDepth, SkiaSharp.SKColorSpace colorspace, SkiaSharp.SKSurfaceProperties props)	Unknown
SkiaSharp.dll!SkiaSharp.SKImage.FromPicture(SkiaSharp.SKPicture picture, SkiaSharp.SKSizeI dimensions)	Unknown
SkiaSharpIssue.dll!SkiaSharpIssue.Program.Main(string[] args) Line 26	C#

The issue is reproduced using any V3 version of the SkiaSharp package. The code works as expected when using the V2 version.

The issue is reproduced both on Windows and Linux (reproduced via Docker).

Code

Visual Studio solution that reproduces the issue: SkiaSharpIssue.zip

Program.cs

using System;
using System.IO;
using SkiaSharp;

namespace SkiaSharpIssue
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SKPicture picture;
            using (var pictureRecorder = new SKPictureRecorder())
            {
                using (var canvas = pictureRecorder.BeginRecording(new SKRect(0, 0, 500, 500)))
                {
                    using (var paint = new SKPaint())
                    {
                        paint.Color = new SKColor(0xFF, 0, 0);
                        canvas.DrawRect(100, 100, 300, 200, paint);
                    }
                }

                picture = pictureRecorder.EndRecording();
            }

            SKBitmap bitmap;
            using (picture)
            using (var image = SKImage.FromPicture(picture, new SKSizeI(500, 500)))
                bitmap = SKBitmap.FromImage(image);

            using (bitmap)
            using (var stream = File.OpenWrite(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "picture.png")))
                bitmap.Encode(stream, SKEncodedImageFormat.Png, 100);
        }
    }
}

SkiaSharpIssue.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
  </PropertyGroup>

  <ItemGroup>

    <!-- DOESN'T WORK -->
    <PackageReference Include="SkiaSharp" Version="3.118.0-preview.2.3" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.118.0-preview.2.3" />

    <!-- DOESN'T WORK -->
    <!--<PackageReference Include="SkiaSharp" Version="3.116.1" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.116.1" />-->

    <!-- DOESN'T WORK -->
    <!--<PackageReference Include="SkiaSharp" Version="3.116.0" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.116.0" />-->

    <!-- DOESN'T WORK -->
    <!--<PackageReference Include="SkiaSharp" Version="3.0.0-preview.0.132" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.0.0-preview.0.132" />-->

    <!-- WORKS -->
    <!--<PackageReference Include="SkiaSharp" Version="2.88.9" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.9" />-->

    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
  </ItemGroup>

</Project>

Expected Behavior

Expected behavior of the code is that the file 'picture.png' should be created.

Actual Behavior

Actual behavior of the code is that the System.AccessViolationException is thrown.

Version of SkiaSharp

3.116.0 (Current)

Last Known Good Version of SkiaSharp

2.88.9 (Previous)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Windows

Platform / Operating System Version

  • Windows 10
  • Linux (via Docker)

Devices

  • Laptop

Relevant Screenshots

No response

Relevant Log Output


Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

StipoR avatar Feb 06 '25 10:02 StipoR

I have exactly the same issue. Any idea when it might be fixed?

Picao84 avatar Apr 01 '25 19:04 Picao84

I have exactly the same issue. Any idea when it might be fixed?

+1, please fix the problem.

BartRennes avatar Apr 02 '25 08:04 BartRennes

I think this may be the fix: https://github.com/mono/skia/pull/154

mattleibow avatar Apr 10 '25 13:04 mattleibow