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

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

Open
1 task done
StipoR opened this issue Feb 6, 2025 · 0 comments
Open
1 task done
Labels

Comments

@StipoR
Copy link

StipoR commented Feb 6, 2025

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

  • I agree to follow this project's Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: New
Development

No branches or pull requests

1 participant