SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

[BUG] Blazor null exception at SkiaSharp.DelegateProxies.SKManagedWStreamWriteProxyImplementation

Open 14z0r opened this issue 1 year ago • 0 comments

Description

I am using an SKSvgCanvas to create svgs. But if I had an exception in my render function and catch them in a try catch block, I get some time later this exception: Object reference not set to an instance of an object. at SkiaSharp.DelegateProxies.SKManagedWStreamWriteProxyImplementation(IntPtr s, Void* context, Void* buffer, IntPtr size)

Code

public class MyRenderer()
{
  public string DrawSVG()
  {
    var stream = new MemoryStream();
    var canvas = SKSvgCanvas.Create(
        new SKRect(
            0,
            0,
            TimelineProperties.TotalWidth,
            TimelineProperties.Size[TimelineSize.M].TotalHeight),
        stream);
  
    DrawSomeStuff(canvas); //if this throws an exception, i later get the exception mentioned above.
    
    canvas.Dispose();
    
    return Encoding.Default.GetString(stream .ToArray());
  }
}

//In my Razor-Page
private void RenderSvg()
{
  var renderer = new MyRenderer();
  try
  {
    svgString = renderer.DrawSVG();
  }
  catch (Exception ex)
  {
    Debug.WriteLine(ex.Message);
  }
  InvokeAsync(() => StateHasChanged());
}


Expected Behavior

Nothing should happen.

Actual Behavior

Exception gets catched and later I get an error:

[UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })]
[return: MarshalAs(UnmanagedType.I1)]
private unsafe static bool SKManagedWStreamWriteProxyImplementation(nint s, void* context, void* buffer, nint size)
{
	GCHandle gch;
	SKAbstractManagedWStream userData = GetUserData<SKAbstractManagedWStream>((nint)context, out gch);
	return userData.OnWrite((nint)buffer, size);
}

System.NullReferenceException: 'Object reference not set to an instance of an object.'

resulting in, the whole Blazor App crashes.

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

Devices

No response

Relevant Screenshots

No response

Relevant Log Output


Code of Conduct

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

14z0r avatar Apr 30 '25 11:04 14z0r