Skip to content

[fix-finder] Fix resource leak in SKImage.FromEncodedData(SKData, SKRectI) due to undisposed intermediate image #3996

@github-actions

Description

@github-actions

Problem

SKImage.FromEncodedData(SKData data, SKRectI subset) creates an intermediate SKImage from encoded data then calls .Subset() on it, but never disposes the intermediate. If Subset() returns a different instance, the intermediate leaks. If it returns the same instance (same-instance-return behavior), disposing the intermediate would destroy the result. The fix must handle both cases using the standard same-instance safety pattern.

Location

  • File(s): binding/SkiaSharp/SKImage.cs
  • Line(s): 158–164

Current Code

public static SKImage FromEncodedData (SKData data, SKRectI subset)
{
    if (data == null)
        throw new ArgumentNullException (nameof (data));

    return FromEncodedData (data)?.Subset (subset);
}

Suggested Fix

public static SKImage FromEncodedData (SKData data, SKRectI subset)
{
    if (data == null)
        throw new ArgumentNullException (nameof (data));

    var image = FromEncodedData (data);
    if (image == null)
        return null;

    var result = image.Subset (subset);
    if (result != image)
        image.Dispose ();

    return result;
}

SkiaSharp Guidelines

  • Does NOT modify any *.generated.cs file
  • Does NOT change existing public API signatures (ABI stable)
  • Does NOT use default parameters in public methods
  • Does NOT use .NET 6+ APIs without #if guards unless they genuinely benefit all callers
  • Follows existing code style and naming conventions

Acceptance Criteria

  • The intermediate image from FromEncodedData(data) is disposed when Subset() returns a different instance
  • The intermediate image is NOT disposed when Subset() returns the same instance
  • Null is returned when FromEncodedData(data) returns null
  • All tests pass: dotnet test tests/SkiaSharp.Tests.Console/SkiaSharp.Tests.Console.csproj
  • No new warnings introduced

Part of #3976

Generated by Nightly Fix Finder · ● 2.4M ·

  • expires on May 22, 2026, 11:42 PM UTC

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

Status
New

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions