Repository navigation
Correctly dispose (again) the managed/native object relationship - #1344
Merged
Merged
Conversation
This is to ensure that when the next call to create a managed object that has the same native handle (such as in a multithreaded environment) the managed object is always brand new. And, no matter what happens to the native object - such as a disposal/recreate or a swap - the manage object is unaffected. Fixes #1343
mattleibow
added a commit
that referenced
this pull request
Jul 3, 2026
Expand the skill's scan taxonomy from 6 to 12 families, each backed by a real historical SkiaSharp leak fix (finalizer/collection ordering #3796/#3291, Clone double-free #2904, disposing native statics #1863/#4080/#1224, field-not-nulled #1256/#1344, stream/callback/delegate-proxy lifetime #3589/#2916/#996, allocation-failure #1784/#1642). Broaden Phase 1.3 de-dup to search by api/type name (real leaks are filed as [BUG], not [memory-leak]) and add the Blob.FromStream / PR #3473 worked example. Extend the Phase 3.2 fix table to cover all 12 families. Document two verified, un-filed family-6 candidates surfaced by a model-diverse scan (SKRegion.SpanIterator missing parent ref; SKPixmap.ExtractSubset/With* not propagating pixelSource). Co-authored-by: Copilot App <[email protected]>
5 of 14 tasks
mattleibow
added a commit
that referenced
this pull request
Sep 7, 2026
[memory-leak] Clarify disposed borrowed-field cleanup (#4994) Context: #1256 Context: #1344 Context: https://github.com/mono/SkiaSharp/actions/runs/33936785050 Focus 8 previously treated a stale field only as an owned native-child cleanup hazard. A disposed wrapper can also retain a caller-owned dependency: the wrapper must not dispose that dependency, but retaining its field keeps the caller object graph alive for as long as the disposed wrapper is reachable. Teach the scanner to separate ownership from retention after wrapper/native teardown. Keep fields rooted while native teardown or callbacks can use them; then dispose owned resources before clearing their fields, and clear borrowed resources without disposing them. Keep the example generalized so a matching field shape alone does not diagnose sibling wrappers as defective. The revised guidance was independently exercised by the focus-8 workflow run. It produced a shipped-package WeakReference probe and an in-repository red-to-green validation, without changing product behavior or public APIs. Co-authored-by: v <[email protected]> Co-authored-by: Matthew Leibowitz <[email protected]> Co-authored-by: Copilot App <[email protected]>
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.
Description of Change
This PR fine tunes the disposal steps of a co-owned object pair. There are a few scenarios:
SKBitmap)When the managed object is disposed, destroy the native object.
SKSurfacecontrolsSKCanvas)When then the managed object destroys the native surface, the native canvas is also destroyed. There may be a case where a brand new native canvas is constructed in the same place - before the managed canvas is disposed. We have to make sure that e dispose the managed object before we destroy the native object.
SKSvgCanvascontrolsSKManagedStream)The managed object creates the native object, but the native object owns the managed object. This means that even when the managed object is disposed, the action is "ignored" and proxied to the native destructor - which will then call back into managed code to dispose the managed object. This is because the native canvas may flush or write closing data into the managed stream.
Scenarios 2 and 3 cause an issue: In one case, the managed
SKCanvasneeds to be destroyed before destroying the nativeSkSurface. On the other hand, the nativeSKSvgCanvasneeds to be destroyed before destroying the managedSKManagedStream. So which goes first?The answer is in the ownership of the handle: In scenario 1, the surface owns its handle and the canvas - which does not own its handle. In scenario 2, the canvas owns its handle and the stream - which does own its handle.
This shows that we need to first dispose all the "reference-only" objects - or the ones that do not own their handle. Then dispose the native object. Finally, clean up any managed objects.
Bugs Fixed
API Changes
None.
Behavioral Changes
See description...
PR Checklist