Skip to content

[memory-leak] Generalize disposed-field ownership guidance - #4994

Merged
mattleibow merged 1 commit into
mainfrom
mattleibow-teach-disposed-reference-cleanup
Sep 7, 2026
Merged

mattleibow merged 1 commit into
mainfrom
mattleibow-teach-disposed-reference-cleanup

Conversation

@mattleibow

@mattleibow mattleibow commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

Generalize memory-leak-fixer focus-area 8 from historical disposal fixes #1256 and #1344.

PR #1256 established that a disposed cached field must be nulled so later access cannot reuse a dead wrapper. PR #1344 established that cleanup order depends on ownership across managed/native object pairs: reference-only children may require managed cleanup before native teardown, while native-owned managed dependencies can remain live until native teardown finishes.

Focus 8 now expresses the general rule derived from those cases:

  • after wrapper/native teardown, clear managed fields that are no longer needed;
  • owned resources are disposed and then nulled;
  • borrowed resources are not disposed, but their stale fields are nulled after teardown;
  • fields remain rooted while native teardown or callbacks can still use them.

The example remains generalized and explicitly does not classify every similarly shaped wrapper as defective. One-leak-per-run, empirical-proof, and open-item deduplication gates remain unchanged.

Related issues

Context: #1256
Context: #1344

Required skia PR

None.

Areas affected

  • Managed API (binding/)
  • Native / C API (externals/skia/src/c, include/c)
  • Generated P/Invoke bindings
  • Native dependency or Skia update (libpng, HarfBuzz, FreeType, zlib, milestone bump, …)
  • Views & integrations (MAUI, Uno, WPF, WinUI, Blazor, …)
  • Rendering output / visual behavior
  • Performance
  • Tests
  • Build, packaging, or CI
  • Documentation or samples

Changes

None — agent guidance only; no public API or runtime behavior changes.

Testing

  • Verified the historical ownership and cleanup rules against merged PRs Null out the field when disposing. #1256 and Correctly dispose (again) the managed/native object relationship #1344.
  • Verified every focus reference link exists.
  • A clean, de-biased focus-8 dry run (33936785050) using GPT-5.6 Terra independently applied the corrected ownership/retention distinction and produced both a shipped-package WeakReference probe and in-repository red→green validation. This confirms the revised guidance directs Terra to the intended analysis without broadening it into a claim about every similarly shaped wrapper.
  • Independent GPT-5.6 Sol code review reported no significant issues.
  • git diff --check passed.

No workflow source changed, so lock recompilation was not required.

Checklist

  • Tests added or updated (not applicable; one-file guidance change grounded in historical merged fixes and independent review)
  • Changes above lists all public API and behavioral changes (or "None.")
  • New/changed public API? Filed a docs issue in mono/SkiaSharp-API-docs so reference docs can be written later (not applicable)
  • Native change? Companion mono/skia PR linked above and bindings regenerated (not applicable)

@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

📦 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 -- 4994

PowerShell / Windows:

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

Step 2 — Add the local NuGet source

dotnet nuget add source ~/.skiasharp/hives/pr-4994/packages --name skiasharp-pr-4994
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-4994

@mattleibow mattleibow changed the title [memory-leak] Clarify borrowed field cleanup guidance [memory-leak] Generalize disposed-field ownership guidance Sep 5, 2026
@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

📊 SkiaSharp benchmarks — PR #4994

⭐ 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 — 🔴 21 slower · 🟢 29 faster

  • 🔴 MatrixMapPointsBenchmark.MapRect(Points: 256) · macOS · 3.29 µs → 8.04 µs (+144%)
  • 🔴 MatrixMapPointsBenchmark.MapPoints(Points: 4096) · Windows · 2.11 µs → 2.99 µs (+42%)
  • 🔴 ColorMathBenchmark.ToColor(Colors: 4096) · Windows · 11.96 µs → 15.85 µs (+33%)
  • 🔴 RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) · Windows · 96.28 µs → 127.10 µs (+32%)
  • 🔴 PathBoundsBenchmark.TightBounds(Points: 1024) · Linux · 10.53 µs → 13.52 µs (+28%)
  • …and 45 more (see details below)
Full per-OS benchmark deltas

Linux

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

Benchmark baseline this PR Δ
PathBoundsBenchmark.TightBounds(Points: 1024) 10.53 µs 13.52 µs 🔴 +28%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 99.12 µs 109.93 µs 🔴 +11%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 2048) 2.92 ms 2.61 ms 🟢 -11%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 1024) 762.64 µs 681.94 µs 🟢 -11%
ColorMathBenchmark.ToColor(Colors: 4096) 15.38 µs 14.25 µs 🟢 -7%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 258.31 µs 275.73 µs 🔴 +7%
MatrixMapPointsBenchmark.MapPoints(Points: 256) 228.9 ns 215.1 ns 🟢 -6%
ColorMathBenchmark.UnPreMultiply(Colors: 4096) 11.45 µs 12.08 µs 🔴 +6%
MatrixMapPointsBenchmark.MapRadius(Points: 256) 14.31 µs 13.57 µs 🟢 -5%

Windows

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

Benchmark baseline this PR Δ
MatrixMapPointsBenchmark.MapPoints(Points: 4096) 2.11 µs 2.99 µs 🔴 +42%
ColorMathBenchmark.ToColor(Colors: 4096) 11.96 µs 15.85 µs 🔴 +33%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 96.28 µs 127.10 µs 🔴 +32%
MatrixMapPointsBenchmark.MapPoints(Points: 256) 167.3 ns 206.4 ns 🔴 +23%
PathBoundsBenchmark.TightBounds(Points: 1024) 16.77 µs 20.32 µs 🔴 +21%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 245.22 µs 285.52 µs 🔴 +16%
PathBoundsBenchmark.TightBounds(Points: 64) 1.69 µs 1.92 µs 🔴 +13%
ColorMathBenchmark.UnPreMultiply(Colors: 4096) 13.20 µs 11.46 µs 🟢 -13%
CanvasDrawBenchmark.Draw(Shapes: 64) 2.58 ms 2.92 ms 🔴 +13%
CanvasDrawBenchmark.Draw(Shapes: 512) 20.56 ms 23.22 ms 🔴 +13%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 1024) 842.00 µs 740.77 µs 🟢 -12%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 2048) 11.47 ms 10.26 ms 🟢 -11%
MatrixOpsBenchmark.Invert(Count: 4096) 151.63 µs 137.18 µs 🟢 -10%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 2048) 3.26 ms 2.95 ms 🟢 -9%
MatrixMapPointsBenchmark.MapRadius(Points: 256) 9.80 µs 10.68 µs 🔴 +9%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 1024) 2.85 ms 2.60 ms 🟢 -9%
MatrixMapPointsBenchmark.MapPoint(Points: 4096) 85.45 µs 78.25 µs 🟢 -8%
MatrixMapPointsBenchmark.MapPoint(Points: 256) 5.34 µs 4.95 µs 🟢 -7%
RuntimeEffectShaderBenchmark.DrawFrame 1.24 ms 1.15 ms 🟢 -7%
MatrixMapPointsBenchmark.MapRadius(Points: 4096) 155.76 µs 166.95 µs 🔴 +7%
ColorMathBenchmark.ToColorF(Colors: 4096) 14.17 µs 13.30 µs 🟢 -6%
ColorParseBenchmark.Parse(Iterations: 1000) 79.17 µs 83.14 µs 🔴 +5%

macOS

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

Benchmark baseline this PR Δ
MatrixMapPointsBenchmark.MapRect(Points: 256) 3.29 µs 8.04 µs 🔴 +144%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 2048) 2.63 ms 2.00 ms 🟢 -24%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 1024) 661.63 µs 516.49 µs 🟢 -22%
BitmapDrawBenchmark.DrawScaledTiles(Tiles: 64) 180.52 µs 209.20 µs 🔴 +16%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 83.79 µs 71.45 µs 🟢 -15%
ColorMathBenchmark.ToColor(Colors: 4096) 18.72 µs 16.12 µs 🟢 -14%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 2048) 5.69 ms 4.90 ms 🟢 -14%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 1024) 1.44 ms 1.25 ms 🟢 -13%
SceneRenderBenchmark.RenderFrame(Complexity: 1) 1.31 ms 1.14 ms 🟢 -12%
MatrixMapPointsBenchmark.MapPoints(Points: 256) 161.6 ns 142.1 ns 🟢 -12%
MatrixMapPointsBenchmark.MapVector(Points: 256) 2.75 µs 2.44 µs 🟢 -11%
SceneRenderBenchmark.RenderFrame(Complexity: 4) 5.94 ms 5.31 ms 🟢 -11%
RuntimeEffectShaderBenchmark.DrawFrame 565.38 µs 509.67 µs 🟢 -10%
BitmapDrawBenchmark.DrawUnscaledTiles(Tiles: 64) 147.84 µs 161.41 µs 🔴 +9%
MatrixMapPointsBenchmark.MapPoint(Points: 256) 1.25 µs 1.14 µs 🟢 -9%
MatrixMapPointsBenchmark.MapPoints(Points: 4096) 1.55 µs 1.67 µs 🔴 +8%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 168.94 µs 156.51 µs 🟢 -7%
MatrixOpsBenchmark.Invert(Count: 4096) 65.05 µs 60.77 µs 🟢 -7%
MatrixOpsBenchmark.Concat(Count: 4096) 112.04 µs 118.31 µs 🔴 +6%

📈 Full interactive perf-dashboard & run details →

@mattleibow
mattleibow merged commit c844464 into main Sep 7, 2026
130 checks passed
@mattleibow
mattleibow deleted the mattleibow-teach-disposed-reference-cleanup branch September 7, 2026 17:32
@github-actions github-actions Bot added this to the 4.154.0-preview.2 milestone Sep 29, 2026

This branch was previously deployed

1 inactive deployment
gh-aw-agents — 27a902ac Deployed Sep 5, 2026 by mattleibow via conclusion #177
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant