Skip to content

[memory-leak] Free GCHandle when a native create with a release delegate fails - #4806

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
dev/memory-leak-gchandle-failed-create-3cffd6a2e4cff2f3
Draft

[memory-leak] Free GCHandle when a native create with a release delegate fails#4806
github-actions[bot] wants to merge 1 commit into
mainfrom
dev/memory-leak-gchandle-failed-create-3cffd6a2e4cff2f3

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Produced by the memory-leak-fixer agentic workflow (SkiaSharp) using the memory-leak-fixer skill. Automated scan → prove → fix of a single native ownership / disposal leak in managed C#.

Changes

Four managed factories that accept a native release/destroy delegate leaked a GCHandle (and the delegate's captured closure) whenever the native create failed:

  • SKImage.FromPixels(SKPixmap, releaseProc, ctx)binding/SkiaSharp/SKImage.cs
  • SKBitmap.InstallPixels(info, pixels, rowBytes, releaseProc, ctx)binding/SkiaSharp/SKBitmap.cs
  • SKData.Create(address, length, releaseProc, ctx)binding/SkiaSharp/SKData.cs
  • SKSurface.Create(info, pixels, rowBytes, releaseProc, ctx, props)binding/SkiaSharp/SKSurface.cs

DelegateProxies.Create allocates GCHandle.Alloc(del), which is freed only inside the native release proc. When the native create returns null/false, native never invokes the release proc, so the handle leaks for the process lifetime. This change captures the GCHandle and frees it on the failure path only (result null/false and a delegate was allocated), never on success — mirroring the existing SKImage.Create(SKImageInfo) precedent. No public signatures change (ABI-safe). No *.generated.cs or externals/skia/** touched.

Required skia PR

None.

Areas Affected

  • SkiaSharp (binding/SkiaSharpSKImage, SKBitmap, SKData, SKSurface)

Testing

  • Leak proven against the shipped NuGet (4.151.1): forcing SKImage.FromPixels to fail via an invalid too-small rowBytes pixmap leaves the release-delegate closure alive after a forced GC, while the success-path control is collected.
  • Fix logic proven red→green: a raw-GCHandle probe mirroring DelegateProxies.Create shows "no free on failure" leaks and "free on failure" collects, in both directions.
  • Regression test added: tests/Tests/SkiaSharp/FailedNativeCreateReleaseDelegateLeakTest.cs forces each factory to fail and asserts the release-delegate closure becomes collectable (AssertEx.EventuallyGC).
  • Note: the in-repo dotnet test could not be executed in the automation sandbox — the repo is on Skia milestone 152 and matching native binaries cannot be bootstrapped there (externals-download and preview-native restore are network-blocked; cached stable natives are milestone 151, rejected by CheckNativeLibraryCompatible). The added test will run in CI where m152 natives are available.

Scope note

Framework bug (a real permanent handle leak), not merely a caller footgun. Leak empirically proven against the shipped NuGet; managed fix logic empirically proven red→green; the in-repo regression test was statically validated but not executed locally due to the sandbox native-bootstrap limitation. ABI: no public signature changes.

Fixes #4805

Generated by Fixer - Memory Leak · opus48 · 585.2 AIC · ⌖ 36.3 AIC · ⊞ 12K ·

When a factory that takes a managed release/destroy delegate (SKImage.FromPixels,
SKBitmap.InstallPixels, SKData.Create, SKSurface.Create) fails, the native release
proc is never invoked, so the GCHandle allocated by DelegateProxies.Create for the
delegate (and its captured closure) leaks for the process lifetime. Free the handle
on the failure path, mirroring the existing SKImage.Create(SKImageInfo) precedent.

Adds a regression test forcing each factory to fail and asserting the release
delegate closure becomes collectable.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added partner/agentic-workflows Issues and PRs created by SkiaSharp agentic workflows. perf/memory-leak Unbounded memory growth: leaked native handles or undisposed objects. Implies tenet/performance. tenet/performance Performance related issues labels Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

partner/agentic-workflows Issues and PRs created by SkiaSharp agentic workflows. perf/memory-leak Unbounded memory growth: leaked native handles or undisposed objects. Implies tenet/performance. tenet/performance Performance related issues

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[memory-leak] GCHandle leaks when a native create with a release delegate fails

0 participants