Skip to content

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

@StipoR

Description

@StipoR

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions