Repository navigation
[memory-leak] Fix GCHandle leak on release-proc factory failure path - #4453
github-actions[bot] wants to merge 1 commit into
Conversation
…4476) Add regression-tracking benchmarks for common colour/interop paths (#4476) Derived from: #4370, #4385, #4442, #4453, #4455 CI: Track - Benchmarks run 29608575980 (green, all OS/roles) The permanent regression-tracking suite (SkiaSharp.Benchmarks.Tracking) landed recently alongside a wave of perf/agentic PRs, but it had no coverage for the managed colour/interop paths those PRs actually touch. This adds a small, curated set of trackers so a future regression (or the improvement itself) shows up as a step in the persisted per-OS time/allocation history. Following the suite's scaled-pyramid philosophy (a few small common-path trackers plus one broad composite, not a micro-benchmark per PR), four benchmarks are added under Tracking/Benchmarks/ (auto-linked into the source-mode project, so they also drive the PR column): * ColorMathBenchmark - managed SKColor->SKColorF (#4370) and SKPMColor PreMultiply/UnPreMultiply (#4385). Zero-alloc; the ToColorF/ToColor ratio (the reverse operator stays native) is a managed-regression signal even on noisy shared runners. These two ports are the real, dashboard-visible speed wins - their time lines should drop when the ports merge. * RasterImageLifecycleBenchmark - create+dispose churn of SKImage.Create raster (#4455) and SKData.Create with a managed release proc (#4453). Allocation is the tracked signal for that object-lifecycle area. * RuntimeEffectShaderBenchmark - a per-frame animated SkSL shader: build uniforms (the SKColor uniform pays the #4370 convert), ToShader, draw. Covers the uniforms lifecycle whose SKData leak #4442 touched. * SceneRenderBenchmark - one broad frame (transforms, linear+radial gradients, filled/stroked primitives, a built path, a scaled image draw, a colour-filter layer, clipping, managed colour maths) so many unrelated future optimizations each nudge it - a merge indicator rather than a micro-benchmark. Scope notes: the failure-path leak fixes (#4453, #4455), the native uniforms leak (#4442), and the UAF/double-free fixes (#4372, #4468) are correctness/native-memory changes that a throughput + managed-allocation dashboard largely will not register; they are represented here by area, not as leak assertions (those belong in soak/ leak tests). #4372 and #4468 are intentionally not benchmarked (niche debug canvases; HarfBuzz lives in a separate assembly the tracking project does not reference). All benchmarks use public API only (the project references the nightly NuGet, not internal SkiaApi), are deterministic and machine-independent (fixed seeds, no fonts or external content), and compile against every benchmarked role - verified against the 3.119.4 baseline (prev-major) and 4.151 nightly, with only SKPath/SKPathBuilder behind the existing #if. A short local run and the full CI matrix (Linux/Windows/ macOS, all version roles plus the source-built PR column) emit time and allocation data for every case. Co-authored-by: Matthew Leibowitz <[email protected]> Co-authored-by: Copilot App <[email protected]>
📦 Try the packages from this PRWarning Do not run these scripts without first reviewing the code in this PR. Step 1 — Download the packages bash / macOS / Linux: curl -fsSL https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.sh | bash -s -- 4453PowerShell / Windows: iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 4453"Step 2 — Add the local NuGet source dotnet nuget add source ~/.skiasharp/hives/pr-4453/packages --name skiasharp-pr-4453More options
Or download manually from Azure Pipelines — look for the Remove the source when you're done: dotnet nuget remove source skiasharp-pr-4453 |
|
📖 Documentation Preview The documentation for this PR has been deployed and is available at: 🔗 View Staging Site This preview will be updated automatically when you push new commits to this PR. This comment is automatically updated by the documentation staging workflow. |
There was a problem hiding this comment.
Pull request overview
Fixes a managed GCHandle leak when native factory methods that use release-proc delegates fail and return null, ensuring the delegate/context handle is freed on the failure path to prevent process-lifetime retention.
Changes:
- Free the
GCHandlecreated byDelegateProxies.Create(...)whenSKImage.FromPixels(...)/SKImage.FromTexture(...)returnnull. - Free the
GCHandlecreated byDelegateProxies.Create(...)when raster-directSKSurface.Create(...)returnsnull. - Free the
GCHandlecreated byDelegateProxies.Create(...)whenSKData.Create(...)returnsnull. - Add regression tests that drive known failure paths and assert the captured context becomes collectible.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/Tests/SkiaSharp/ReleaseProcHandleLeakTest.cs | Adds regression tests that fail on the leaky failure-path behavior and pass once handles are freed. |
| binding/SkiaSharp/SKSurface.cs | Ensures release-proc GCHandle is freed when raster-direct surface creation fails and returns null. |
| binding/SkiaSharp/SKImage.cs | Ensures release-proc GCHandle is freed when raster/texture image creation fails and returns null. |
| binding/SkiaSharp/SKData.cs | Ensures release-proc GCHandle is freed when data creation fails and returns null. |
📊 SkiaSharp benchmarks — PR #4453⭐ this PR (full source build) vs 🌙 nightly · Linux · Windows · macOS
Highlights⏱️ Time — 🔴 45 slower · 🟢 18 faster
Full per-OS benchmark deltasLinux⏱️ Time (vs 🌙 nightly
Windows⏱️ Time (vs 🌙 nightly
macOS⏱️ Time (vs 🌙 nightly
|
📦 Artifact size reportPackages from this PR (build Total Packages
+38 package(s) unchanged (< 50.0 KB). Per-file changes
|
| File | Size |
|---|---|
runtimes/win-x86/native/libHarfBuzzSharp.pdb |
21.9 MB → 22.2 MB (🔴 +272.0 KB) |
runtimes/win-arm64/native/libHarfBuzzSharp.pdb |
22.3 MB → 22.5 MB (🔴 +184.0 KB) |
SkiaSharp.NativeAssets.WinUI
| File | Size |
|---|---|
runtimes/win-x64/native/libGLESv2.pdb |
66.6 MB → 66.6 MB (🟢 −64.0 KB) |
runtimes/win-arm64/native/libGLESv2.pdb |
64.5 MB → 64.4 MB (🟢 −32.0 KB) |
runtimes/win-x86/native/libGLESv2.pdb |
67.2 MB → 67.2 MB (🟢 −32.0 KB) |
Informational only — this never blocks the PR. Native binaries are labelled by os/arch.
When SKImage.FromPixels/FromTexture, SKSurface.Create and SKData.Create wrap a managed release delegate in a GCHandle and hand it to native code, native frees that handle by invoking the release proc on destruction. If the native factory fails (returns null), the release proc is never invoked and the GCHandle - plus the delegate and captured context it roots - leaks for the process lifetime. Free the GCHandle when the native call returns null and a delegate was supplied. Co-authored-by: Copilot <[email protected]>
e23b3cb to
e207bd3
Compare
Problem
SKImage.FromPixels,SKImage.FromTexture,SKSurface.Create(raster direct) andSKData.Createwrap a managed release delegate in aGCHandle(viaDelegateProxies.Create) and pass it to native code. Native frees that handle by invoking the release proc when the wrapped native object is destroyed. When the native factory fails and returns null, no object is created, the release proc never fires, and theGCHandle— plus the delegate and the caller's capturedcontext— leaks for the process lifetime.Fix
Free the
GCHandlewhen the native factory returns null and a delegate was supplied:Managed-C# only (
binding/**), no public signature change — ABI stable.SKBitmap.InstallPixelsis intentionally not touched: native calls its release proc itself on failure (freeing the handle would double-free — verified empirically).Proof (red → green)
New regression test
tests/Tests/SkiaSharp/ReleaseProcHandleLeakTest.csdrives the failure path (unknownSKColorType/ too-smallrowBytes) with a release proc capturing a tracked context, then asserts the context is collected.SKImage/SKSurface/SKData/SKBitmaptest classes remain green.Test command used on the runner:
Scope note
Framework bug (real leak on the allocation-failure path), not just a footgun. Empirically proven for
SKImage.FromPixelsandSKSurface.Create; statically reasoned (identical pattern) forSKImage.FromTextureandSKData.Create. ABI-safe, internal-only change.Fixes #4452