Leak hints, SwiftUI hosting and nested tracking (3.3) - #20
Open
DanielCech wants to merge 1 commit into
Open
DanielCech wants to merge 1 commit into
DanielCech wants to merge 1 commit into
Conversation
- Leak messages list likely causes from the leaked object's stored properties: closures, tasks, Combine subscriptions, timers and property cycles - .hosting lifecycle shows a SwiftUI view in a test window (UIKit and AppKit) - trackForDeallocation inside expectDeallocation joins its check - Tests for hints, SwiftUI and nested tracking; README and CHANGELOG Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.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.
Why
#19 made dealloc tests short and reported leaks at the right line. The next questions are why something leaked, and how to test the code most new screens are written in: SwiftUI.
What's new
Leak messages say what to look at
When an object leaks, DeallocTests now looks at its stored properties and lists the usual suspects. Here's the sample app's deliberate leak:
It points out:
Tasks, Combine subscriptions and timers,self.child.parentpointing back to the object.If nothing suspicious is found, you get the general advice as before.
SwiftUI:
.hostingSwiftUI views are values, so the thing that leaks is the object behind them, usually the view model.
.hostingshows the view in a test window, letsonAppearand.taskrun, then removes it:The tests check both sides: work in
.taskis cancelled with the view and passes, while aTaskstarted inonAppearand never cancelled is caught (and the hint names it). It works on iOS and macOS.Checking owned objects too
Inside an
expectDeallocationclosure,trackForDeallocationadds objects to the same check. They must go away together with the tested object:This works in Swift Testing and XCTest.
A deliberate design decision
I looked at automatically checking every object reachable from the tested one, as XCTAssertNoLeak does. I left it out on purpose. Swift's reflection (
Mirror) can't tell aweakproperty from a strong one, so a screen's weak delegate (in the sample app, the coordinator) would be reported as a leak. False failures would make people distrust the tool. Instead:trackForDeallocation.The README says plainly that hints are suggestions, not proof.
Tests
swift test): all suites pass, including the new hint, SwiftUI and nested-tracking tests.someClosurehint, and the weakflowDelegateis correctly not flagged.Not in this PR (4.0)
DeallocTesterin favour ofexpectDeallocation.DefaultInitializable.🤖 Generated with Claude Code