Swift 6.4: Warning for Weak Captures Within Nested Closures
New in Swift 6.4 […] is a warning which is emitted when a nested closure performs a
weakcapture while the parent closure implicitly captures that object as a strong reference.At first glance, that new diagnostic could be a bit puzzling, since it might seem unnecessary to have to specify such a weak capture twice (or, alternatively, move it to the parent closure’s capture list), but it turns out that it’s an incredibly useful new warning that can help us avoid a quite common kind of memory management bug.
[…]
Finally, in situations where capturing
selfstrongly doesn’t actually lead to a retain cycle, we can silence the new warning for nestedweakcaptures by explicitly addingselfto the outer closure’s capture list, such as when creating aTaskwithin the following example[…]
Previously:
- Swift 6.4
- Xcode 27 Announced
- How to Automate Memory Leak Detection With XCTest
- Avoiding Implicit Retain Cycles When Using Swift Function References
- Weak Self: Closure Rules of Thumb
- Swift “guard” Capture Specifier Pitch
- Implicit Capturing of Self in Swift 5.3
- Dealing With Weak in Closure-based Delegation
- Stop the weak-strong Swift Dance
1 Comment RSS · Twitter · Mastodon
I am so glad I've moved back to a language with a modern garbage collector.
When you're seeing a "quite common kind of memory management bug", is it possible your memory model is just too complex for us mortal programmers? We as an industry know how to have the computer manage all this for us.
It's true an automatic GC isn't quite as efficient, but it's OK to spend a few CPU cycles on improving our quality of life. That's been kind of the Macintosh's whole M.O. from day one.