Tuesday, September 1, 2026

Swift 6.4: Warning for Weak Captures Within Nested Closures

John Sundell:

New in Swift 6.4 […] is a warning which is emitted when a nested closure performs a weak capture 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 self strongly doesn’t actually lead to a retain cycle, we can silence the new warning for nested weak captures by explicitly adding self to the outer closure’s capture list, such as when creating a Task within the following example[…]

Previously:

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.

Leave a Comment