Skip to content

[FEATURE] Controlled finalization for GPU-bound objects #1442

Open
@kekekeks

Description

@kekekeks

Problem:

While most of the SkiaSharp objects can be safely disposed from any thread, GPU bound ones need the corresponding GPU context to be active on disposing thread. It's fine as long as the object lifetime is controlled by application, but Dispose might be called from the finalizer thread in following cases:

  1. Main has exited
  2. Environment.Exit(...) is called
  3. Object was somehow leaked and collected by GC without being properly disposed

Libraries using SkiaSharp can deal with the third one by thoroughly controlling the object lifetimes, but unfortunately have no control over the application control flow. That leads to crashes such as AvaloniaUI/Avalonia#4423

Proposed solution:

Instead of calling Dispose directly, call a user-specified callback that could properly enqueue the object for disposal on a proper thread or make the GPU context to be current. Such callback should be specified on object creation, e. g.

public static SKSurface Create (GRContext context, bool budgeted, SKImageInfo info, Action<Action> disposeCallback)

Finalizer should do something like this:

~SKSurface()
{
  if(_disposeCallback != null)
    _disposeCallback(() => Dispose(false));
  else
    Dispose(false);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions