UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to figure out which swift closure is holding strong reference?

Forums > Swift

I am trying to build a fairly complex app in Swift and I have a classic problem of retain cycle. My ViewController is not getting removed from memory, because there is a closure that is holding strong reference to it. But in memory graph, I really dont get much detail about which closure it is. I mean individual classes at least have specific names, but closure are just this obscure Swift Closure Context

Any tricks I am missing here?


Instead of isolating which closure is holding the strong reference in this situation, I believe a much safer solution would be to make the reference to the view controller weak in every closure's capture list. Why would a closure need to retain a reference to a view controller after the view controller (and the view it manages) has disappeared?

If you are using the view controller as a "conduit" to pass to a closure a reference to a property that survives the disappearance of the view controller, hopefully you can refactor your code to pass to the closure a reference to that property independent of the view controller.


I am still new to this entire Swift thing, so I will take your advice at face value.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.