[memory-leak] Free GCHandle when a native create with a release delegate fails - #4806
Draft
github-actions[bot] wants to merge 1 commit into
Draft
[memory-leak] Free GCHandle when a native create with a release delegate fails#4806github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.csSKBitmap.InstallPixels(info, pixels, rowBytes, releaseProc, ctx)—binding/SkiaSharp/SKBitmap.csSKData.Create(address, length, releaseProc, ctx)—binding/SkiaSharp/SKData.csSKSurface.Create(info, pixels, rowBytes, releaseProc, ctx, props)—binding/SkiaSharp/SKSurface.csDelegateProxies.CreateallocatesGCHandle.Alloc(del), which is freed only inside the native release proc. When the native create returnsnull/false, native never invokes the release proc, so the handle leaks for the process lifetime. This change captures theGCHandleand frees it on the failure path only (result null/false and a delegate was allocated), never on success — mirroring the existingSKImage.Create(SKImageInfo)precedent. No public signatures change (ABI-safe). No*.generated.csorexternals/skia/**touched.Required skia PR
None.
Areas Affected
binding/SkiaSharp—SKImage,SKBitmap,SKData,SKSurface)Testing
SKImage.FromPixelsto fail via an invalid too-smallrowBytespixmap leaves the release-delegate closure alive after a forced GC, while the success-path control is collected.GCHandleprobe mirroringDelegateProxies.Createshows "no free on failure" leaks and "free on failure" collects, in both directions.tests/Tests/SkiaSharp/FailedNativeCreateReleaseDelegateLeakTest.csforces each factory to fail and asserts the release-delegate closure becomes collectable (AssertEx.EventuallyGC).dotnet testcould not be executed in the automation sandbox — the repo is on Skia milestone 152 and matching native binaries cannot be bootstrapped there (externals-downloadand preview-native restore are network-blocked; cached stable natives are milestone 151, rejected byCheckNativeLibraryCompatible). 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