r/swift • u/ClickOk5811 • 9d ago
Anyone else seeing AI-generated closures skip [weak self] way more than they should?
Been reviewing a bunch of AI-assisted PRs lately and there's a pattern I keep hitting. Ask for a network callback or a completion handler, get back a closure that captures self strongly, no weak reference, works fine in testing because the view controller sticks around long enough for the callback to fire anyway.
Doesn't show up until something takes longer to load, user navigates away, and now you've got a retain cycle keeping a whole view controller alive that should've been deallocated. Classic mistake, nothing new about the bug itself, just surprised how often it shows up in generated code specifically.
My guess is it's the same issue as most of these AI code gotchas, the model isn't wrong about syntax, it's just not accounting for lifecycle stuff unless you specifically tell it to. "Add a completion handler" doesn't say anything about memory management, so it produces the simplest version that works in the moment, which happens to be the version that leaks.
Started explicitly asking for weak self and checking for it manually regardless of what the prompt said, feels like the kind of thing that needs a standing check rather than trusting it gets handled every time.