SkiaSharp: [BUG] Crashes in sk_canvas_restore_to_count on Mac 10.15

Apps using SKGLView sporadically abort() when SkiaSharp calls sk_canvas_restore_to_count with this typical stack trace:

Thread 0 Crashed:: tid_307  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	0x00007fff6f20e7fa __pthread_kill + 10
1   libsystem_pthread.dylib       	0x00007fff6f2cbbc1 pthread_kill + 432
2   libsystem_c.dylib             	0x00007fff6f195a1c abort + 120
3   com.kruegersystems.circuitmac 	0x0000000108fd3518 altstack_handle_and_restore.cold.1 + 40
4   com.kruegersystems.circuitmac 	0x0000000108ce490c altstack_handle_and_restore + 188
5   libSkiaSharp.dylib            	0x000000010927d784 0x10924c000 + 202628
6   ???                           	0x000000011062618e 0 + 4569850254
7   ???                           	0x000000010f7e8b4e 0 + 4554918734	  at <unknown> <0xffffffff>

	  at SkiaSharp.SkiaApi:sk_canvas_restore_to_count <0x000ad>
	  at SkiaSharp.SKCanvas:RestoreToCount <0x0005a>
	  at SkiaSharp.SKAutoCanvasRestore:Restore <0x00052>
	  at SkiaSharp.SKAutoCanvasRestore:Dispose <0x00042>
	  at SkiaSharp.Views.Mac.MySKGLView:DrawRect <0x005d0>
	  at <Module>:runtime_invoke_void__this___CGRect <0x000fd>
	  at <unknown> <0xffffffff>
	  at AppKit.NSApplication:NSApplicationMain <0x001a4>
	  at AppKit.NSApplication:Main <0x00112>
	  at Circuit.Mac.MainClass:Main <0x00052>
	  at <Module>:runtime_invoke_void_object <0x000b0>

All crashes are here: https://gist.github.com/praeclarum/d5dbcda0782daa0ae81b494969b4d93f

Code

new SKGLView();

Expected Behavior

No crash

Actual Behavior

Crash after some period of time. It always crashes, but can take awhile.

Basic Information

  • Version with issue: 1.68.2.preview21
  • IDE: Visual Studio for Mac
  • Platform Target Frameworks:
    • macOS: 10.15

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

@gregsn you can try the new packages (preview 45) on NuGet.org.

I am getting the same issue. canvas.RestoreToCount (saveCount); called in SKAutoCanvasRestore.Dispose() is making my SKLGLControl.OnPaint() crash on … Windows.

I managed to repro this in seconds! I just created like a billion windows 😃 Let me have a closer look now…

@mattleibow I did it! I made a minimal repro! 😄

Steps:

  1. Run the attached project under the debugger.
  2. Create a few more windows with Cmd+N
  3. Drink some coffee, read Twitter
  4. Observe crash

In the following screenshot, it crashed after 1500 frames, or about 25 seconds. I put a GC.Collect thread in it just to speed things up. I’m guessing a finalizer is doing something naughty.

I should also note that I updated macOS to the latest 10.15.3 and have the same problem so I don’t think it’s Catalina’s fault.

SkiaSharpIssue1121.zip

Screenshot 2020-02-12 12 23 18