Friday, June 20, 2025

Subtle Swift Retain Cycle

Paul Samuels:

The problem we have looking at the scope is that the strong reference is in scope until the end of the function, but we have our repeat loop before the end of the function so we will never get to the end.

[…]

You could write a unit to verify that the reference does not leak something like this.

Previously:

1 Comment RSS · Twitter · Mastodon


It's funny how anything old is new again. People who transitioned to ARC developed an instinct to think about where things were captured and potential cycles. With Swift, Apple decided to deemphasize all the complexities (while creating infinitely more as time and "evolution" and "SwiftUWhy" went on), and so trivial things like this are perplexing to Swift developers.

> Retain cycles are a pain and the ol’ chuck a weak on things doesn’t always work so it’s worth writing tests and using instruments to hunt things down.

I miss the days when people wrote interesting technical blog posts, not these low level, weak nonsense posts with even more nonsense conclusions. Tests will save you indeed.

Leave a Comment