Skip to content

[memory-leak] Dispose SKStreamAsset in ToHarfBuzzBlob non-memory-backed path - #4710

Merged
mattleibow merged 2 commits into
mainfrom
dev/memory-leak-tohbblob-asset-dispose-02820c5124ded5fd
Aug 20, 2026
Merged

mattleibow merged 2 commits into
mainfrom
dev/memory-leak-tohbblob-asset-dispose-02820c5124ded5fd

Conversation

@github-actions

@github-actions github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes an undisposed native-handle leak in BlobExtensions.ToHarfBuzzBlob(this SKStreamAsset).

For non-memory-backed streams, ToHarfBuzzBlob copies bytes into Marshal.AllocCoTaskMem and hands a release callback to the Blob. That callback previously only freed the CoTaskMem and never disposed the consumed SKStreamAsset, leaking its native handle until finalization. The memory-backed branch already disposes the asset (() => asset.Dispose()); this change makes the else-branch consistent.

Changes

  • source/SkiaSharp.HarfBuzz/SkiaSharp.HarfBuzz/BlobExtensions.cs: dispose asset in the non-memory-backed Blob release callback.
  • tests/Tests/SkiaSharp/SKShaperTest.cs: add regression test ToHarfBuzzBlobDisposesNonMemoryBackedStream.

Required skia PR

None.

Areas Affected

  • SkiaSharp.HarfBuzz

Testing

Red→green proven with the new in-repo test:

  • Without the fix: FAILED (asset not disposed after blob release).
  • With the fix: PASSED.
  • Neighboring SKShaperTest suite: 8/8 passed.

No rendering change, so no screenshots.

Scope note

Framework bug (inconsistent native-handle ownership between the two branches), empirically proven via a red→green regression test. ABI-safe — internal behavior only, no signature change.


Produced automatically by the SkiaSharp agentic memory-leak-fixer workflow + memory-leak-fixer skill.

Fixes #4709

Generated by Fixer - Memory Leak · opus48 · 482 AIC · ⌖ 33.9 AIC · ⊞ 12K · ◷

… path

The else-branch of ToHarfBuzzBlob (for non-memory-backed streams) copied
bytes into CoTaskMem but its Blob release callback only freed the CoTaskMem
and never disposed the consumed SKStreamAsset, leaking its native handle
until finalization. Mirror the memory-backed branch by disposing the asset
in the release callback.

Co-authored-by: Copilot <[email protected]>
@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 8, 2026
@mattleibow
mattleibow marked this pull request as ready for review August 20, 2026 23:04
@github-actions

Copy link
Copy Markdown
Contributor Author

📦 Try the packages from this PR

Warning

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 -- 4710

PowerShell / Windows:

iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 4710"

Step 2 — Add the local NuGet source

dotnet nuget add source ~/.skiasharp/hives/pr-4710/packages --name skiasharp-pr-4710
More options
Option Description
--successful-only / -SuccessfulOnly Only use successful builds
--force / -Force Overwrite previously downloaded packages
--list / -List List available artifacts without downloading
--build-id ID / -BuildId ID Download from a specific build

Or download manually from Azure Pipelines — look for the nuget artifact on the build for this PR.

Remove the source when you're done:

dotnet nuget remove source skiasharp-pr-4710

@mattleibow
mattleibow merged commit 9268648 into main Aug 20, 2026
2 of 3 checks passed
@mattleibow
mattleibow deleted the dev/memory-leak-tohbblob-asset-dispose-02820c5124ded5fd branch August 20, 2026 23:04
@github-actions

Copy link
Copy Markdown
Contributor Author

📊 SkiaSharp benchmarks — PR #4710

⭐ this PR (full source build) vs 🌙 nightly · Linux · Windows · macOS

Informational only — this never blocks the PR. 🟢 faster / less allocation · 🔴 slower / more allocation; moves under 5% are hidden as noise.

⏱️ Times are raw BenchmarkDotNet means, and the ⭐ PR and baseline legs run on separate CI runners, so microbenchmarks can swing run-to-run — treat small time deltas as noise. Allocations are deterministic and the reliable signal. The interactive perf-dashboard (linked below) applies smoothing for the trend view.

Highlights

⏱️ Time — 🔴 36 slower · 🟢 62 faster

  • 🔴 ColorMathBenchmark.ToColor(Colors: 4096) · Windows · 9.22 µs → 16.98 µs (+84%)
  • 🔴 RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) · Windows · 71.48 µs → 125.09 µs (+75%)
  • 🔴 PathBoundsBenchmark.TightBounds(Points: 1024) · Windows · 12.63 µs → 20.13 µs (+59%)
  • 🔴 PathBoundsBenchmark.TightBounds(Points: 64) · Windows · 1.18 µs → 1.88 µs (+59%)
  • 🔴 MatrixMapPointsBenchmark.MapPoints(Points: 256) · Windows · 132.0 ns → 204.1 ns (+55%)
  • …and 93 more (see details below)
Full per-OS benchmark deltas

Linux

⏱️ Time (vs 🌙 nightly 4.152.0-nightly.29)

Benchmark baseline this PR Δ
ColorMathBenchmark.ToColor(Colors: 4096) 14.71 µs 9.04 µs 🟢 -39%
MatrixMapPointsBenchmark.MapPoints(Points: 256) 249.4 ns 167.1 ns 🟢 -33%
MatrixMapPointsBenchmark.MapPoints(Points: 4096) 2.83 µs 2.07 µs 🟢 -27%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 2048) 2.96 ms 2.21 ms 🟢 -25%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 1024) 759.66 µs 570.88 µs 🟢 -25%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 264.03 µs 200.40 µs 🟢 -24%
ColorParseBenchmark.Parse(Iterations: 1000) 76.67 µs 59.53 µs 🟢 -22%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 2048) 8.05 ms 6.25 ms 🟢 -22%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 1024) 2.06 ms 1.60 ms 🟢 -22%
BitmapDrawBenchmark.DrawScaledTiles(Tiles: 256) 1.37 ms 1.08 ms 🟢 -21%
BitmapDrawBenchmark.DrawUnscaledTiles(Tiles: 64) 259.39 µs 206.60 µs 🟢 -20%
BitmapDrawBenchmark.DrawUnscaledTiles(Tiles: 256) 964.01 µs 769.64 µs 🟢 -20%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 99.20 µs 79.37 µs 🟢 -20%
BitmapDrawBenchmark.DrawScaledTiles(Tiles: 64) 358.18 µs 288.11 µs 🟢 -20%
SceneRenderBenchmark.RenderFrame(Complexity: 1) 3.95 ms 3.19 ms 🟢 -19%
PathBoundsBenchmark.TightBounds(Points: 64) 969.3 ns 786.2 ns 🟢 -19%
CanvasDrawBenchmark.Draw(Shapes: 512) 33.29 ms 27.12 ms 🟢 -19%
SceneRenderBenchmark.RenderFrame(Complexity: 4) 18.15 ms 14.81 ms 🟢 -18%
CanvasDrawBenchmark.Draw(Shapes: 64) 4.16 ms 3.40 ms 🟢 -18%
MatrixMapPointsBenchmark.MapRadius(Points: 256) 14.32 µs 11.73 µs 🟢 -18%
RuntimeEffectShaderBenchmark.DrawFrame 2.85 ms 2.35 ms 🟢 -18%
MatrixMapPointsBenchmark.MapVector(Points: 4096) 176.23 µs 145.24 µs 🟢 -18%
PathBoundsBenchmark.TightBounds(Points: 1024) 10.56 µs 8.85 µs 🟢 -16%
ColorMathBenchmark.ToColorF(Colors: 4096) 13.12 µs 11.00 µs 🟢 -16%
MatrixMapPointsBenchmark.MapVector(Points: 256) 10.84 µs 9.13 µs 🟢 -16%
…and 9 more

Windows

⏱️ Time (vs 🌙 nightly 4.152.0-nightly.29)

Benchmark baseline this PR Δ
ColorMathBenchmark.ToColor(Colors: 4096) 9.22 µs 16.98 µs 🔴 +84%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 71.48 µs 125.09 µs 🔴 +75%
PathBoundsBenchmark.TightBounds(Points: 1024) 12.63 µs 20.13 µs 🔴 +59%
PathBoundsBenchmark.TightBounds(Points: 64) 1.18 µs 1.88 µs 🔴 +59%
MatrixMapPointsBenchmark.MapPoints(Points: 256) 132.0 ns 204.1 ns 🔴 +55%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 191.59 µs 287.33 µs 🔴 +50%
CanvasDrawBenchmark.Draw(Shapes: 512) 15.94 ms 22.80 ms 🔴 +43%
CanvasDrawBenchmark.Draw(Shapes: 64) 2.00 ms 2.85 ms 🔴 +42%
MatrixMapPointsBenchmark.MapPoints(Points: 4096) 1.61 µs 2.27 µs 🔴 +41%
MatrixMapPointsBenchmark.MapRadius(Points: 4096) 119.08 µs 165.54 µs 🔴 +39%
MatrixMapPointsBenchmark.MapRadius(Points: 256) 7.49 µs 10.35 µs 🔴 +38%
ColorParseBenchmark.Parse(Iterations: 1000) 60.28 µs 82.92 µs 🔴 +38%
SceneRenderBenchmark.RenderFrame(Complexity: 4) 7.82 ms 10.26 ms 🔴 +31%
MatrixMapPointsBenchmark.MapVector(Points: 256) 6.66 µs 8.75 µs 🔴 +31%
SceneRenderBenchmark.RenderFrame(Complexity: 1) 1.65 ms 2.17 ms 🔴 +31%
MatrixMapPointsBenchmark.MapRect(Points: 4096) 104.39 µs 136.13 µs 🔴 +30%
MatrixMapPointsBenchmark.MapRect(Points: 256) 6.24 µs 8.02 µs 🔴 +28%
MatrixMapPointsBenchmark.MapVector(Points: 4096) 107.38 µs 137.54 µs 🔴 +28%
BitmapDrawBenchmark.DrawUnscaledTiles(Tiles: 256) 756.81 µs 962.18 µs 🔴 +27%
BitmapDrawBenchmark.DrawScaledTiles(Tiles: 64) 273.67 µs 344.08 µs 🔴 +26%
MatrixOpsBenchmark.Concat(Count: 4096) 197.41 µs 248.18 µs 🔴 +26%
BitmapDrawBenchmark.DrawScaledTiles(Tiles: 256) 1.04 ms 1.30 ms 🔴 +25%
RuntimeEffectShaderBenchmark.DrawFrame 914.02 µs 1.14 ms 🔴 +24%
BitmapDrawBenchmark.DrawUnscaledTiles(Tiles: 64) 205.87 µs 253.79 µs 🔴 +23%
ColorMathBenchmark.UnPreMultiply(Colors: 4096) 9.95 µs 12.21 µs 🔴 +23%
…and 9 more

macOS

⏱️ Time (vs 🌙 nightly 4.152.0-nightly.29)

Benchmark baseline this PR Δ
BitmapDrawBenchmark.DrawScaledTiles(Tiles: 256) 822.18 µs 690.72 µs 🟢 -16%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 76.04 µs 88.16 µs 🔴 +16%
BitmapDrawBenchmark.DrawScaledTiles(Tiles: 64) 221.77 µs 193.29 µs 🟢 -13%
MatrixMapPointsBenchmark.MapRect(Points: 4096) 54.35 µs 47.70 µs 🟢 -12%
CanvasDrawBenchmark.Draw(Shapes: 64) 1.59 ms 1.41 ms 🟢 -11%
BitmapDrawBenchmark.DrawUnscaledTiles(Tiles: 256) 610.43 µs 543.07 µs 🟢 -11%
MatrixMapPointsBenchmark.MapVector(Points: 4096) 44.09 µs 39.27 µs 🟢 -11%
PathBoundsBenchmark.TightBounds(Points: 64) 965.6 ns 866.3 ns 🟢 -10%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 1024) 1.59 ms 1.43 ms 🟢 -10%
CanvasDrawBenchmark.Draw(Shapes: 512) 12.35 ms 11.14 ms 🟢 -10%
PathBoundsBenchmark.TightBounds(Points: 1024) 9.05 µs 8.19 µs 🟢 -10%
MatrixMapPointsBenchmark.MapVector(Points: 256) 2.78 µs 2.52 µs 🟢 -9%
MatrixMapPointsBenchmark.MapPoint(Points: 4096) 20.08 µs 18.18 µs 🟢 -9%
BitmapDrawBenchmark.DrawUnscaledTiles(Tiles: 64) 165.87 µs 150.63 µs 🟢 -9%
MatrixMapPointsBenchmark.MapRadius(Points: 256) 6.79 µs 6.17 µs 🟢 -9%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 2048) 2.90 ms 2.64 ms 🟢 -9%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 1024) 731.55 µs 665.10 µs 🟢 -9%
ColorMathBenchmark.UnPreMultiply(Colors: 4096) 8.34 µs 7.59 µs 🟢 -9%
MatrixOpsBenchmark.Invert(Count: 4096) 71.69 µs 65.26 µs 🟢 -9%
MatrixMapPointsBenchmark.MapPoint(Points: 256) 1.25 µs 1.14 µs 🟢 -9%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 2048) 6.28 ms 5.74 ms 🟢 -9%
MatrixMapPointsBenchmark.MapRadius(Points: 4096) 107.21 µs 98.09 µs 🟢 -9%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 164.86 µs 151.30 µs 🟢 -8%
MatrixOpsBenchmark.Concat(Count: 4096) 120.80 µs 111.02 µs 🟢 -8%
ColorMathBenchmark.ToColor(Colors: 4096) 20.70 µs 19.08 µs 🟢 -8%
…and 5 more

📈 Full interactive perf-dashboard & run details →

@github-actions

Copy link
Copy Markdown
Contributor Author

📦 Artifact size report

Packages from this PR (build 1561501) vs the latest nightly baseline 4.152.0-nightly.17 (observed 2026-08-20).

Total .nupkg size: 569.8 MB → 568.8 MB (−1.0 MB, -0.2%)

Packages

⚠️ marks growth over 500.0 KB or 2%. Changes under 50.0 KB are treated as noise.

Package baseline this PR Δ Δ%
SkiaSharp.NativeAssets.WinUI 109.1 MB 108.3 MB 🟢 −881.7 KB -0.8%
SkiaSharp.NativeAssets.Android 18.1 MB 17.9 MB 🟢 −256.8 KB -1.4%
SkiaSharp.NativeAssets.WebAssembly 69.7 MB 69.7 MB 🔴 +59.1 KB +0.1%

+37 package(s) unchanged (< 50.0 KB).

Per-file changes

SkiaSharp.NativeAssets.Android

File Size
🧬 android-x86 (runtimes/android-x86/native/libSkiaSharp.so) 11.8 MB → 11.0 MB (🟢 −802.2 KB)
README.md 219 B → 1.9 KB (🔴 +1.7 KB)

SkiaSharp.NativeAssets.WebAssembly

File Size
README.md 219 B → 1.9 KB (🔴 +1.7 KB)

SkiaSharp.NativeAssets.WinUI

File Size
runtimes/win-x64/native/libGLESv2.pdb 66.6 MB → 66.5 MB (🟢 −96.0 KB)
runtimes/win-arm64/native/libGLESv2.pdb 64.5 MB → 64.4 MB (🟢 −80.0 KB)
runtimes/win-x86/native/libGLESv2.pdb 67.2 MB → 67.2 MB (🟢 −48.0 KB)
README.md 219 B → 1.9 KB (🔴 +1.7 KB)

Informational only — this never blocks the PR. Native binaries are labelled by os/arch.

@github-actions github-actions Bot added this to the 4.152.0-rc.1 milestone Sep 7, 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: Done

Development

Successfully merging this pull request may close these issues.

[memory-leak] Undisposed SKStreamAsset native handle in ToHarfBuzzBlob non-memory-backed path

1 participant